Welcome Guest, Not a member yet? Register   Sign In
Form Submit Problem
#1

[eluser]mcrobertstd[/eluser]
On form submission, the form returns itself with input fields cleared.
This is the view "listing_address".
Code:
<form action='/add-new-property/save_listing_address' method='POST'>

  <div class='TDFormHeader'>Enter Property Address</div>
  <table width='100%' cellPadding='3' cellSpacing='2' class='TDForm'>
  
   <tr>
    <td valign='top' width='150'><span class='FormTitle'>Address:</span></td>
    <td>&lt;input type='text' name='address' value="&lt;?php echo set_value('address');?&gt;"&gt;&lt;div class='ErrorDiv'>&lt;?php echo form_error('address'); ?&gt;</div></td>
   </tr>
  
   <tr>
    <td valign='top' width='150'><span class='FormTitle'>Space Number:</span></td>
    <td>&lt;input type='text' name='space' value="&lt;?=set_value('space');?&gt;"&gt;&lt;div class='ErrorDiv'>&lt;?php echo form_error('space'); ?&gt;</div></td>
   </tr>
  
   <tr>
    <td valign='top' width='150'><span class='FormTitle'>City:</span></td>
    <td>&lt;input type='text' name='city' value="&lt;?=set_value('city');?&gt;"&gt;&lt;div class='ErrorDiv'>&lt;?php echo form_error('city'); ?&gt;</div></td>
   </tr>
  
   <tr>
    <td valign='top' width='150'><span class='FormTitle'>State:</span></td>
    <td>&lt;?=$this->system_vars->state_array_select_box(set_value('state'));?&gt;<div class='ErrorDiv'><div class='ErrorDiv'>&lt;?php echo form_error('state'); ?&gt;</div></td>
   </tr>
  
   <tr>
    <td valign='top' width='150'>&nbsp;</td>
    <td>&lt;input type='submit' name='sub' value="Continue"&gt;&lt;/td>
   </tr>  
  </table>

&lt;/form&gt;
The controller action is
Code:
function save_listing_address(){
                                      $this->load->library('form_validation');

  $this->form_validation->set_rules('address', 'Address', 'trim|required|xss_clean');
  $this->form_validation->set_rules('space', 'Space', 'trim|xss_clean');
  $this->form_validation->set_rules('city', 'City', 'trim|required|xss_clean');
  $this->form_validation->set_rules('state', 'State', 'trim|required|xss_clean|exact_length[2]');
  
  if ($this->form_validation->run()){  
   $AddressPlotter = $this->system_vars->address_plotter(set_value('address'),set_value('city'),set_value('state'));
   if(isset($AddressPlotter)){
  
    if(isset($AddressPlotter['warning'])){
    
     $GenerateError = "<b>ADDRESS ERROR !!!</b><br>The address you have entered seems to be invalid.";
     if(isset($AddressPlotter['Address']) && isset($AddressPlotter['City']) && isset($AddressPlotter['State'])) $GenerateError .= "<br><u>The closes match is:</u> ".$AddressPlotter['Address'].", ".$AddressPlotter['City'].", ".$AddressPlotter['State']." ".$AddressPlotter['Zip'];
    
     $this->session->set_flashdata('response',$GenerateError);
     redirect('/add_new_property');
    
    }else{
    
     $NPArray['address'] = $AddressPlotter['Address'];
     $NPArray['space'] = set_value('space');
     $NPArray['city'] = $AddressPlotter['City'];
     $NPArray['state'] = $AddressPlotter['State'];
     $NPArray['zip'] = $AddressPlotter['Zip'];
     $NPArray['lon'] = $AddressPlotter['Longitude'];
     $NPArray['lat'] = $AddressPlotter['Latitude'];
    
     $this->session->set_userdata('np_address', $NPArray);
     redirect('/add_new_property/community_select');
    
    }
  
   }else{
  
    $this->form_validation->set_message('check_address', "<b>ADDRESS ERROR !!!</b><br>The address you have entered does NOT exist.<br>");
    return false;
  
   }
  
  }else{
  
   $this->load->view('elements/header_view');
   $this->load->view('add_new_property/listing_address');
   $this->load->view('elements/footer_view');
  
  }

}
The form reloads because the form_validator is seeing no post data and returning false. I tried echoing the post data at the top-level index.php and find the post is blank there, as well.
The CodeIgniter version is 1.7.2. Removing the form_validation allows the script to continue to the next page.
The expected result is that with form validation, the website moves on to the next screen and not resubmit the same page with no data.
#2

[eluser]InsiteFX[/eluser]
Code:
if ($this->form_validation->run() == FALSE){
#3

[eluser]mcrobertstd[/eluser]
Thanks for the quick response. I believe if the form_validation fails if would return false, so I would want to test for not false.
Code:
function save_listing_address(){
    $this->load->library('form_validation');
  $this->form_validation->set_rules('address', 'Address', 'trim|required|xss_clean');
  $this->form_validation->set_rules('space', 'Space', 'trim|xss_clean');
  $this->form_validation->set_rules('city', 'City', 'trim|required|xss_clean');
  $this->form_validation->set_rules('state', 'State', 'trim|required|xss_clean|exact_length[2]');
  
  if ($this->form_validation->run() != FALSE){  
   $AddressPlotter = $this->system_vars->address_plotter(set_value('address'),set_value('city'),set_value('state'));
   if(isset($AddressPlotter)){
  
    if(isset($AddressPlotter['warning'])){
    
     $GenerateError = "<b>ADDRESS ERROR !!!</b><br>The address you have entered seems to be invalid.";
     if(isset($AddressPlotter['Address']) && isset($AddressPlotter['City']) && isset($AddressPlotter['State'])) $GenerateError .= "<br><u>The closes match is:</u> ".$AddressPlotter['Address'].", ".$AddressPlotter['City'].", ".$AddressPlotter['State']." ".$AddressPlotter['Zip'];
    
     $this->session->set_flashdata('response',$GenerateError);
     redirect('/add_new_property');
    
    }else{
    
     $NPArray['address'] = $AddressPlotter['Address'];
     $NPArray['space'] = set_value('space');
     $NPArray['city'] = $AddressPlotter['City'];
     $NPArray['state'] = $AddressPlotter['State'];
     $NPArray['zip'] = $AddressPlotter['Zip'];
     $NPArray['lon'] = $AddressPlotter['Longitude'];
     $NPArray['lat'] = $AddressPlotter['Latitude'];
    
     $this->session->set_userdata('np_address', $NPArray);
     redirect('/add_new_property/community_select');
    
    }
  
   }else{
  
    $this->form_validation->set_message('check_address', "<b>ADDRESS ERROR !!!</b><br>The address you have entered does NOT exist.<br>");
    return false;
  
   }
  
  }else{
  
   $this->load->view('elements/header_view');
   $this->load->view('add_new_property/listing_address');
   $this->load->view('elements/footer_view');
  
  }

}

Here's the function in the form_validation:
Code:
function run($group = '')
{
  // Do we even have any data to process?  Mm?
  if (count($_POST) == 0)
  {
   return FALSE;
  }
  
  // Does the _field_data array containing the validation rules exist?
  // If not, we look to see if they were assigned via a config file
  if (count($this->_field_data) == 0)
  {
   // No validation rules?  We're done...
   if (count($this->_config_rules) == 0)
   {
    return FALSE;
   }
  
   // Is there a validation rule for the particular URI being accessed?
   $uri = ($group == '') ? trim($this->CI->uri->ruri_string(), '/') : $group;
  
   if ($uri != '' AND isset($this->_config_rules[$uri]))
   {
    $this->set_rules($this->_config_rules[$uri]);
   }
   else
   {
    $this->set_rules($this->_config_rules);
   }

I know that the run function is failing and returning FALSE because count($_POST) is 0. It seems like data in the $_POST array is being cleared. Any ideas on that? Help is appreciated.

Thanks,
mcrobertstd
#4

[eluser]mcrobertstd[/eluser]
I found the issue to be in the routing.
Code:
&lt;form action='/add_new_property/save_listing_address' method='POST'&gt;

  <div class='TDFormHeader'>Enter Property Address</div>
  <table width='100%' cellPadding='3' cellSpacing='2' class='TDForm'>
  
   <tr>
    <td valign='top' width='150'><span class='FormTitle'>Address:</span></td>
    <td>&lt;input type='text' name='address' value="&lt;?php echo set_value('address');?&gt;"&gt;&lt;div class='ErrorDiv'>&lt;?php echo form_error('address'); ?&gt;</div></td>
   </tr>
  
   <tr>
    <td valign='top' width='150'><span class='FormTitle'>Space Number:</span></td>
    <td>&lt;input type='text' name='space' value="&lt;?=set_value('space');?&gt;"&gt;&lt;div class='ErrorDiv'>&lt;?php echo form_error('space'); ?&gt;</div></td>
   </tr>
  
   <tr>
    <td valign='top' width='150'><span class='FormTitle'>City:</span></td>
    <td>&lt;input type='text' name='city' value="&lt;?=set_value('city');?&gt;"&gt;&lt;div class='ErrorDiv'>&lt;?php echo form_error('city'); ?&gt;</div></td>
   </tr>
  
   <tr>
    <td valign='top' width='150'><span class='FormTitle'>State:</span></td>
    <td>&lt;?=$this->system_vars->state_array_select_box(set_value('state'));?&gt;<div class='ErrorDiv'><div class='ErrorDiv'>&lt;?php echo form_error('state'); ?&gt;</div></td>
   </tr>
  
   <tr>
    <td valign='top' width='150'>&nbsp;</td>
    <td>&lt;input type='submit' name='sub' value="Continue"&gt;&lt;/td>
   </tr>  
  </table>

&lt;/form&gt;

Form Action Corrected to
Code:
&lt;form action='/add-new-property/save-listing-address' method='POST'&gt;

The router was mapping the original url of the form post to an error controller.
Thanks for the responses...mcrobertstd
#5

[eluser]InsiteFX[/eluser]
Also this is wrong if your base_url is setup correct:
Code:
&lt;form action='/add_new_property/save_listing_address' method='POST'&gt;

// should be!
&lt;form action='add_new_property/save_listing_address' method='POST'&gt;
#6

[eluser]CroNiX[/eluser]
@InsiteFX The first one should work ok. The second one wouldn't as that's relative to the current page (he didn't use base_url())
#7

[eluser]InsiteFX[/eluser]
@CroNiX,

Thanks, but I was not sure if he was using the base_url or not. but I do know what your saying.




Theme © iAndrew 2016 - Forum software by © MyBB