• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
POST data not being sent from a form to a controller

#1
[eluser]Rurik[/eluser]
hey guys,

so I have this view here, as shown below, and for some reason, when clicking submit, the controller doesn't receive any $_POST data from the form.

var_dump($POST) shows empty array.

Any ideas why this might be happening?

Rurik

Code:
<?php echo form_open('job/update/', array('id' => 'update', 'name' => 'update')); ?>

<div class="images">

  &lt;?php echo img('application/images/updateJob.png');?&gt;

</div>

<h1>Update Job</h1>
<br><br><br><br>


<div class="fieldset">

  &lt;?php
   if (isset($error))
   {
    echo '<div id ="error">';
    echo '<h2>Error:</h2>';
    echo '<p>'.$error.'</p>';
    echo '</div>';
   }
  ?&gt;

  &lt;input type='hidden' name='c' id='c' value='&lt;?php if(!empty($view[0])) { echo $view[0]; } else { echo "0"; }    ?&gt;'&gt;
  <div class="left">
   <h4>Job Details:</h4>
   <div>
    <label for="job_number">Job No:</label>
    &lt;input type="text" name="job_number" id="job_number" value="&lt;?php echo $job['jobno']; ?&gt;" disabled="disabled"&gt;
   </div>

   <div>
    <label for="date">Date:</label>
    &lt;input type="text" name="date" id="date" value="&lt;?php echo $job['datetime']-&gt;format('d-M-Y'); ?&gt;"&gt;
   </div>

   <div>
    <label for="council">Council:</label>
    &lt;input type="text" name="council" id="council" value="&lt;?php echo htmlspecialchars($job['council']); ?&gt;"&gt; *
   </div>
  
   <div>
    <label for="dano">DA No:</label>
    &lt;input type="text" name="dano" id="dano" value="&lt;?php echo htmlspecialchars($job['dano']); ?&gt;"&gt;
   </div>

   <div>
    <label for="client">Client:</label>
    &lt;input type="text" name="client" id="client" value="&lt;?php echo $job['clientid']; ?&gt;" disabled="disabled"&gt;  
   </div>
  
  </div>


  <div class="right">
   <h4>Location:</h4>
   <label for="street_no">Street No:</label>
   &lt;input type="text" name="street_no" id="street_no" value="&lt;?php echo $address['streetno']; ?&gt;"&gt; *
   <br>

   <label for="street_name">Street Name:</label>
   &lt;input type="text" name="street_name" id="street_name" value="&lt;?php echo $address['streetname']; ?&gt;"&gt; *
   <br>

   <label for="street_type">Street Type:</label>
   <select name="street_type" id="street_type">
   &lt;?php
    foreach ($street_types as $type)
    {
     echo '<option';
     if ($type == $address['streettype']) echo ' selected="selected"';
     echo '>'.$type.'</option>';
    }
   ?&gt;
   </select> *
   <br>

   <label for="suburb">Suburb:</label>
   &lt;input type="text" name="suburb" id="suburb" value="&lt;?php echo $address['suburb']; ?&gt;"&gt; *
   <br>

   <label for="postcode">Postcode:</label>
   &lt;input type="text" name="postcode" id="postcode" value="&lt;?php echo $address['postcode']; ?&gt;"&gt; *
   <br>

   <label for="state">State:</label>
   &lt;input type="text" name="state" id="state" value="&lt;?php echo $address['state']; ?&gt;"&gt; *
   <br>

   <label for="country">Country:</label>
   &lt;input type="text" name="country" id="country" value="&lt;?php echo $address['country']; ?&gt;"&gt;
   <br>

   <label for="dp">DP:</label>
   &lt;input type="text" name="dp" id="dp" value="&lt;?php echo $address['dp']; ?&gt;"&gt;
   <br>

   <label for="lot_no">Lot No:</label>
   &lt;input type="text" name="lot_no" id="lot_no" value="&lt;?php echo $address['lotno']; ?&gt;"&gt;
   <br>

   <label for="po_box">PO Box:</label>
   &lt;input type="text" name="po_box" id="po_box" value="&lt;?php echo $address['pobox']; ?&gt;"&gt;
  
  </div>

  
  <div>
   &lt;input type="submit" id="submit" name="submit" value="Submit" class="button"&gt;
   <p>* = Required fields</p>
  </div>

</div>

&lt;?php echo form_close();?&gt;

#2
[eluser]solid9[/eluser]
post your controller codes here.

#3
[eluser]Rurik[/eluser]
No problem, should have thought to do that in the first place.

Code:
/**
  * Update an existing job.
  * @param int $job_number  The number of the job to update.
  */
public function update($job_number=0)
{
  $this->load->model('Job_model');
     $job  = array();
  $address = array();
  
  // Get post data.
  $job['joblocation'] = '';
  $job['jobno']  = $this->input->post('job_number');
  $job['datetime'] = new DateTime($this->input->post('date'));
  $job['dano']  = $this->input->post('dano');
  $job['council']  = $this->input->post('council');
  echo $job['jobno'];
  echo $job['dano'];
  echo $job['council'];
  $address['streetno'] = $this->input->post('street_no');
  $address['streetname'] = $this->input->post('street_name');
  $address['suburb']  = $this->input->post('suburb');
  $address['country']  = $this->input->post('country');
  $address['postcode'] = $this->input->post('postcode');
  $address['state']  = $this->input->post('state');
  $address['dp']   = $this->input->post('dp');
  $address['lotno']  = $this->input->post('lot_no');
  $address['pobox']  = $this->input->post('po_box');
  $address['streettype'] = $this->input->post('street_type');
  echo "here2";
  var_dump($_POST);
  if (isset($_POST['submit']))
  {
   $this->Job_model->update($job);
   echo "here";
   redirect('job/');
  }

  // Otherwise, get the data from the database.  
  else
  {
   $job = $this->Job_model->search($job_number);
   $job = $job[0];
   $job['datetime'] = new DateTime($job['datetime']);
   $address = $this->Job_model->get_address($job['joblocation']);
   $address = $address[0];
  }
  

  // Get the street types.
  $street_types = array();
  $this->load->model('staff_model');
  $streets = $this->staff_model->get_street_types();

  foreach ($streets as $street)
  {
   $street_types[$street['streettype']] = $street['streettype'];
  }

  
  // Load the client list.
  $clients = array();
  $this->load->model('client_model');
  $people = $this->client_model->get_client_person_list();
  $companies = $this->client_model->get_client_company_list();
  

  // Allocate view data.
  $viewdata = array();
  $viewdata['job'] = $job;
  $viewdata['street_types'] = $street_types;
  $viewdata['address'] = $address;
  $viewdata['people'] = $people;
  $viewdata['companies'] = $companies;
  
  $this->layout->view('job/update', $viewdata);
}


/**
  * Create a new job
  */
public function add()
{
  $this->load->model('Job_model');
     $job  = array();
  $address = array();
  
  // Get post data.
  $job['joblocation'] = '';
  $job['jobno']  = $this->input->post('job_number');
  $job['datetime'] = new DateTime($this->input->post('date'));
  $job['dano']  = $this->input->post('dano');
  $job['council']  = $this->input->post('council');
  $job['turnaround'] = $this->input->post('turnaround');
  $job['clientid'] = $this->input->post('clientNo');
  
  $address['streetno'] = $this->input->post('street_no');
  $address['streetname'] = $this->input->post('street_name');
  $address['suburb']  = $this->input->post('suburb');
  $address['country']  = $this->input->post('country');
  $address['postcode'] = $this->input->post('postcode');
  $address['state']  = $this->input->post('state');
  $address['dp']   = $this->input->post('dp');
  $address['lotno']  = $this->input->post('lot_no');
  $address['pobox']  = $this->input->post('po_box');
  $address['streettype'] = $this->input->post('street_type');
  
  
  if (isset($_POST['submit']))
  {
   $this->Job_model->insert($address, $job);
   redirect('/job');
  }


  // Get the street types.
  $street_types = array();
  $this->load->model('staff_model');
  $streets = $this->staff_model->get_street_types();

  foreach ($streets as $street)
  {
   $street_types[$street['streettype']] = $street['streettype'];
  }

  

  // Allocate view data.
  $viewdata = array();
  $viewdata['job'] = $job;
  $viewdata['street_types'] = $street_types;
  $viewdata['address'] = $address;

  
  $this->layout->view('job/add', $viewdata);
}

the add function works, the update doesn't. And form helper is loaded as part of auto loadaer.

#4
[eluser]TWP Marketing[/eluser]
[quote author="Rurik" date="1350105546"]hey guys,

Code:
...
&lt;?php echo form_open('job/update/'.$job['jobno'], array('id' => 'update', 'name' => 'update')); ?&gt;
...
[/quote]

Your form action method needs to include the job number parameter (see snippet above) which the controller method is defaulting to '0' unless you pass it in form_open().

However, that doesn't explain why the form vars are not in the POST array.

I see that the name of your input field name 'job_number', is not the same as the original var name $job['job_no'] It doesn't necessarily need to be the same, but could be confusing.

#5
[eluser]Rurik[/eluser]
Good point, and fixed, but as you say, that doesnt explain why POST array is empty. I also changed job_number to be jobno like the variable, but no changes, though I also didnt expect that to change it.

#6
[eluser]TWP Marketing[/eluser]
You might check your application/config/config.php and note the setting for the get array:
Code:
...
$config['allow_get_array'] = FALSE;
...

I don't know if it is the problem, but as the documentation states, it can cause trouble if set to TRUE.

Also, try using CI's helper to read the POST array:

Code:
$post_array = $this->input->post(); // returns all POST items without XSS filter
print_r($post_array);
[EDIT] put this in your update view

#7
[eluser]Rurik[/eluser]
Well the server seems to not be responding at the moment and not letting me connect to it via winscp, so I will test this later and post the results.

#8
[eluser]Rurik[/eluser]
Get array was indeed set to true, I have changed this while noting that it may cause some problems as I dont know if anywhere is using get. I dont think it is, but better safe then sorry.

print_r prints out nothing, but var_dump of the same variable prints out bool(false).

But otherwise, no change, still not updating, and so I assume still no data.

#9
[eluser]TWP Marketing[/eluser]
[quote author="Rurik" date="1350196132"]
...
print_r prints out nothing, but var_dump of the same variable prints out bool(false).

But otherwise, no change, still not updating, and so I assume still no data.[/quote]

The boolean false is the correct response from CI's input class helper, which means it is working but not finding any fields in the post array.

Since the correct controller is being executed, that means the form_open action parameter is working correctly now.

It may not be important, but I think all form input field statements should escape the end carat: '/>' rather than just '>'. You are coding those by hand, rather than using CI's helper: "form_input()", which would automatically escape the end carat '/>'. The hand coding is fine, I do it myself sometimes, but the escape might be a problem.

#10
[eluser]Rurik[/eluser]
ok, so heres an interesting change. I almost always handcode, though I didnt code this page directly myself, that was handled by my team mate, but after putting in those escapes, the print_r($postdata) no longer prints anything out.

Ok, scrap that, I just remembered print_r didnt print anything out, it was var_dump that did.

var_dump still returns bool(false)


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.