EllisLab text mark
Advanced Search
1 of 2
1
   
POST data not being sent from a form to a controller
Posted: 13 October 2012 01:19 AM   [ Ignore ]
Joined: 2012-08-21
88 posts

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

<?php echo form_open('job/update/', array('id' => 'update''name' => 'update')); ?>
 
 
<div class="images">
 
  
<?php echo img('application/images/updateJob.png');?>
 
 
</div>
 
 <
h1>Update Job</h1>
 <
br><br><br><br>
 
 
 <
div class="fieldset">
 
  
<?php
   
if (isset($error))
   
{
    
echo '<div id ="error">';
    echo 
'<h2>Error:</h2>';
    echo 
'<p>'.$error.'</p>';
    echo 
'</div>';
   
}
  ?>
 
  
<input type='hidden' name='c' id='c' value='<?php if(!empty($view[0])) { echo $view[0]; } else { echo "0"; }    ?>'>
  <
div class="left">
   <
h4>Job Details:</h4>
   <
div>
    <
label for="job_number">Job No:</label>
    <
input type="text" name="job_number" id="job_number" value="<?php echo $job['jobno']; ?>" disabled="disabled">
   </
div>
 
   <
div>
    <
label for="date">Date:</label>
    <
input type="text" name="date" id="date" value="<?php echo $job['datetime']->format('d-M-Y'); ?>">
   </
div>

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

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


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

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

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

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

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

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

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

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

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

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

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

<?php echo form_close();?> 
 
Posted: 13 October 2012 06:09 AM   [ Ignore ]   [ # 1 ]   [ Rating: 0 ]
Avatar
Joined: 2011-08-08
524 posts

post your controller codes here.

 Signature 

Stick with it, practice it and have fun with it.

 
Posted: 13 October 2012 06:12 AM   [ Ignore ]   [ # 2 ]   [ Rating: 0 ]
Joined: 2012-08-21
88 posts

No problem, should have thought to do that in the first place.

/**
  * 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.

 
Posted: 13 October 2012 06:58 PM   [ Ignore ]   [ # 3 ]   [ Rating: 0 ]
Joined: 2008-12-19
612 posts
Rurik - 13 October 2012 01:19 AM

hey guys,

...
<?php echo form_open('job/update/'.$job['jobno'], array('id' => 'update''name' => 'update')); ?>
... 

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.

 Signature 

CI 2.1.3, Linux, LAMP. We also like Gold and Silver…
Blame the hammer, it is obviously the guilty party…
User Guide? Nobody told me there was a User Guide!

 
Posted: 13 October 2012 07:09 PM   [ Ignore ]   [ # 4 ]   [ Rating: 0 ]
Joined: 2012-08-21
88 posts

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.

 
Posted: 13 October 2012 07:26 PM   [ Ignore ]   [ # 5 ]   [ Rating: 0 ]
Joined: 2008-12-19
612 posts

You might check your application/config/config.php and note the setting for the get array:

...
$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:

$post_array $this->input->post(); // returns all POST items without XSS filter
print_r($post_array); 

[EDIT] put this in your update view

 Signature 

CI 2.1.3, Linux, LAMP. We also like Gold and Silver…
Blame the hammer, it is obviously the guilty party…
User Guide? Nobody told me there was a User Guide!

 
Posted: 13 October 2012 07:36 PM   [ Ignore ]   [ # 6 ]   [ Rating: 0 ]
Joined: 2012-08-21
88 posts

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.

 
Posted: 14 October 2012 02:28 AM   [ Ignore ]   [ # 7 ]   [ Rating: 0 ]
Joined: 2012-08-21
88 posts

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.

 
Posted: 14 October 2012 02:37 PM   [ Ignore ]   [ # 8 ]   [ Rating: 0 ]
Joined: 2008-12-19
612 posts
Rurik - 14 October 2012 02:28 AM

...
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.

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.

 Signature 

CI 2.1.3, Linux, LAMP. We also like Gold and Silver…
Blame the hammer, it is obviously the guilty party…
User Guide? Nobody told me there was a User Guide!

 
Posted: 14 October 2012 04:08 PM   [ Ignore ]   [ # 9 ]   [ Rating: 0 ]
Joined: 2012-08-21
88 posts

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)

 
Posted: 14 October 2012 06:45 PM   [ Ignore ]   [ # 10 ]   [ Rating: 0 ]
Avatar
Joined: 2009-02-19
3796 posts

Try different settings for

$config['uri_protocol'

in
/application/config/config.php

You could also try putting a print_r($_POST); at the top of your index.php file to see if post is making it through before CI gets ahold of it.

 Signature 
 
Posted: 14 October 2012 10:01 PM   [ Ignore ]   [ # 11 ]   [ Rating: 0 ]
Joined: 2012-08-21
88 posts
CroNiX - 14 October 2012 06:45 PM

Try different settings for

$config['uri_protocol'

in
/application/config/config.php

You could also try putting a print_r($_POST); at the top of your index.php file to see if post is making it through before CI gets ahold of it.

This makes no sense to me, though in the first case that would be because I dont understand how the uri_protocol option affects things.

but the print_r in the index makes no sense, as other pages can handle post data, just not this one and one other for some reason. Especially seeing as how all of this is behind CI’s front door so to speak.

Again, this could be me not understanding how CI does things.

 
Posted: 14 October 2012 11:54 PM   [ Ignore ]   [ # 12 ]   [ Rating: 0 ]
Avatar
Joined: 2009-02-19
3796 posts

Where exactly are you checking to see if you lost your post data?  Early on in your update method, you check if the form was submitted and if it was you redirect just after updating (some) of your data in your model.  Redirecting wipes out the POST array (new request to server) so if you’re trying to access it after the redirect you can’t.

Also, you shouldn’t be using $_POST directly, use $this->input->post(‘field’)

 Signature 
 
Posted: 15 October 2012 12:02 AM   [ Ignore ]   [ # 13 ]   [ Rating: 0 ]
Joined: 2012-08-21
88 posts

not to be rude, but if you read through the code, you would notice that I check the post before the redirect, and that it cant get into the earlier redirect, because post is empty, which means submit is not set. You would also note that I dont use $_POST directly, and that I do use $this->input->post(‘field’);

I only used $_POST for testing if the array was there at all, via var dump

 
Posted: 15 October 2012 12:11 AM   [ Ignore ]   [ # 14 ]   [ Rating: 0 ]
Avatar
Joined: 2009-02-19
3796 posts

Ah, I must have imagined this line while I wasn’t reading through your convoluted code:

if (isset($_POST['submit'])) 

I’ll leave you to it - sorry to bother you.

 Signature 
 
Posted: 15 October 2012 12:14 AM   [ Ignore ]   [ # 15 ]   [ Rating: 0 ]
Joined: 2012-08-21
88 posts

oh, that one instance, my mistake. As I said, this is not my original code, but code I have been tasked with extending. This is, as far as I know, the only time this is used in the controllers, and either way it does not get into there in the first place and is so not the cause of the problem.

 
1 of 2
1