Welcome Guest, Not a member yet? Register   Sign In
Form validation requires double submit

When I enable my form validation routine my form requires 2 clicks of the submit button.

If I fill out the form so validation fails, then my errors don't show up on form reload unless I click Submit a 2nd time. Same thing with sending once validation is met, it sends the email, but doesn't show my send confirmation message on form reload unless I click Submit a 2nd time. (which also sends a 2nd, but blank, email from the form)

Anyone else encountered this? I'm not posting any code in case this is a known issue, but if you would like to see any of the code let me know which parts and I will post them in a follow up.


post code please. Smile

views/contact.php (excerpt)
<div id="contact-form">
      <h2>Contact Us</h2>
      if ($this->session->flashdata('errors') != '') {
        echo "<div class=\"alert\">";
        echo $this->session->flashdata('errors');
        echo "</div>";
      if ($this->session->flashdata('messagestatus') != '') {
        echo "<div class=\"notification\">";
        echo $this->session->flashdata('messagestatus');
        echo "</div>";
      echo form_open('email/contact_send');
      echo form_hidden('formname', 'Contact_Us Form');
      echo form_label('Name', 'name');
      echo form_input('name', $this->input->post('name'));
      echo form_label('Phone', 'phone');
      echo form_input('phone', $this->input->post('phone'));
      echo form_label('Email', 'email');
      echo form_input('email', $this->input->post('email'));
      echo form_label('Reason', 'reason');
      $data = array(
        'name' => 'reason',
        'rows' => '6',
        'cols' => '35'
      echo form_textarea($data, $this->input->post('reason'));
      echo form_submit('submit', 'Send Request');
      echo form_close();
    </div> &lt;!-- end contact-form --&gt;

controller/email.php (excerpt)
function contact_send() {
    /* The Contact Form was submitted   */

    // load the form validation library

    //setup validation rules on form fields
    $this->form_validation->set_rules('name', 'Name', 'trim|required');
    $this->form_validation->set_rules('phone', 'Phone', 'trim|required');
    $this->form_validation->set_rules('email', 'Email Address', 'trim|required|valid_email');
    $this->form_validation->set_rules('reason', 'Reason for contact request', 'trim|required|');

    // run form validation
    if($this->form_validation->run() == TRUE)
// validator has passed - compile the email
      $messagebody = "Name: ";
      $messagebody .= $this->input->post('name');
      $messagebody .= "<br>Phone: ";
      $messagebody .= $this->input->post('phone');
      $messagebody .= "<br>E-Mail: ";
      $messagebody .= $this->input->post('email');
      $messagebody .= "<br>Reason: ";
      $messagebody .= $this->input->post('reason');
      $msg_subject = 'Contact Form Request';
      $msg_valid_send = 'Thank you for your contact request';
      $msg_error_send = 'Contact form message did not process properly';

      /* Validation&Message; construction   */
      /* are done so send the message  */
// create array to store smtp values
      $config = Array(
        'protocol' => $this->config->item('smtp_protocol'),
        'smtp_host' => $this->config->item('smtp_host'),
        'smtp_port' => $this->config->item('smtp_port'),
        'smtp_user' => $this->config->item('smtp_user'),
        'smtp_pass' => $this->config->item('smtp_pass'),
        'mailtype' => $this->config->item('smtp_mailtype'),
        'charset' => $this->config->item('smtp_charset'),
        'recipient' => $this->config->item('smtp_recipient'));

// load the email library class
      $this->load->library('email', $config);

// set to, from, message, etc

// attempt to send the email
      if ($result = $this->email->send()) {
        $message = $msg_valid_send;
      } else {
        $message = $msg_error_send;

      $this->session->set_flashdata('messagestatus', $message);

// send user back to the appropriate form
    } else {
      // form failed to validate so pass the errors
      // to flashdata and then return user to form
      $this->session->set_flashdata('errors', validation_errors());

views/includes/template.php (complete)
NOTE: variables are stored in session which will return the user to the last page they were on (main_content variable), the variable is adusted in the main controller function calls for page navigation

  <div id="main_content">
    &lt;?php $this->load->view($this->session->userdata('main_content')); ?&gt;


there is nothing wrong w/ the form. Smile

just try to fresh re-install CI.

Theme © iAndrew 2016 - Forum software by © MyBB