Welcome Guest, Not a member yet? Register   Sign In
Reciving post data in a array then printing out in a view again
#1

[eluser]density5[/eluser]
Hi, guys this might seem a simple procedure but I have confused my self.

In my Controller - Which I need to pick up the “post” data and then put it in to another array, in order to post back to the view. I have no idea, on how to grab the array data from the post array.

I have in my view : Example

Code:
<li>
<label for="Title">Title<em> <img src="http://somesite/ci/assets/images/required_star.gif"
alt="required" /></em>&lt;?php echo form_error('customer[title]'); ?&gt;</label>
<select name="customer[title]">
<option value="" &lt;?php echo set_select('customer[title]', '', TRUE); ?&gt; >Select a Title</option>
<option value="mr" &lt;?php echo set_select('customer[title]', 'mr'); ?&gt; >Mr</option>
<option value="ms" &lt;?php echo set_select('customer[title]', 'ms'); ?&gt; >Ms</option>
<option value="mrs" &lt;?php echo set_select('customer[title]', 'mrs'); ?&gt; >Mrs</option>
<option value="miss" &lt;?php echo set_select('customer[title]', 'miss'); ?&gt; >Miss</option>
</select>
</li>

I have in my Controller - Which I need to pick up and “post” data and then put in to another array, in order to post back to the view.
Code:
&lt;?php
class Form extends Controller {

        function index()
        {
        $this->load->helper('form');
        $this->load->helper('url');
        $this->load->helper('array');
        $this->load->helper('html');
        $this->load->helper('date');
        $this->load->library('form_validation');
        $this->load->library('email');

// Cant seem to get post data with these
        $data['title'] = $_POST['customer[title]'];
        $data['title'] = $_POST['title'];
        $data_title = $_POST['customer[title]'];

        if ($this->form_validation->run('booking') == FALSE)
        {
        $data['title'] = "";
        $data['message'] = "";
        $this->load->view('myform' , $data);
        }
        else
        {
// This below line was a test to try and populate the array element :(
        $data['title'] = "$data_title";
        $data['message'] = "Thank you, your booking has been sent!";
        $this->load->view('myform' , $data);
        }

    } // Close Function

I know i am going to kick myself on this.
#2

[eluser]n0xie[/eluser]
As far as I know using [] in a form field indicates that it is a POST array. You could check this by doing a var_dump($_POST). My suggestion is to not use [] as form field names, since in this case it offers no benefit to send an array: a client can only be 1 of the options.
#3

[eluser]Evil Wizard[/eluser]
Code:
<li>
<label for="Title">Title<em> <img src="http://somesite/ci/assets/images/required_star.gif"
alt="required" /></em>&lt;?php echo form_error('customer[title][]'); ?&gt;</label>
<select name="customer[title][]">
<option value="" &lt;?php echo set_select('customer[title][]', '', TRUE); ?&gt; >Select a Title</option>
<option value="mr" &lt;?php echo set_select('customer[title][]', 'mr'); ?&gt; >Mr</option>
<option value="ms" &lt;?php echo set_select('customer[title][]', 'ms'); ?&gt; >Ms</option>
<option value="mrs" &lt;?php echo set_select('customer[title][]', 'mrs'); ?&gt; >Mrs</option>
<option value="miss" &lt;?php echo set_select('customer[title][]', 'miss'); ?&gt; >Miss</option>
</select>
</li>
Code:
&lt;?php
class Form extends Controller {

        function index()
        {
        $this->load->helper('form');
        $this->load->helper('url');
        $this->load->helper('array');
        $this->load->helper('html');
        $this->load->helper('date');
        $this->load->library('form_validation');
        $this->load->library('email');

// Cant seem to get post data with these
/*        $data['title'] = $_POST['customer[title]'];
        $data['title'] = $_POST['title'];
        $data_title = $_POST['customer[title]'];
*/
    // I think that will work
    $data['customer']['title'] = $this->input->post('title');
    // This should bring back the $_POST['customer'] value even if it is an array
     $data['customer'] = $this->input->post('customer');  
    
    if ($this->form_validation->run('booking') == FALSE)
        {
        $data['title'] = "";
        $data['message'] = "";
        $this->load->view('myform' , $data);
        }
        else
        {
// This below line was a test to try and populate the array element :(
        $data['title'] = "$data_title";
        $data['message'] = "Thank you, your booking has been sent!";
        $this->load->view('myform' , $data);
        }

    } // Close Function
I hope that helps
#4

[eluser]density5[/eluser]
Nice Guys - I have reverted back to just standard inputs for now, thank you n0xie :0) its working - But I really do need the config with arrays “rules” inside aswell as this gives me more flexibility in my controller, and I would like 2 rules checking 2 sets of fields on the forms in one view.

I am also going to try Evil Wizards way and see if I can get it to work – I did try and pickup from the get post before but with out any luck – I am sure

Code:
$data['customer'] = $this->input->post('customer');

Will work.
Thank you for the help Evil W. Your php gods!




Theme © iAndrew 2016 - Forum software by © MyBB