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

[eluser]Tekamba[/eluser]
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.

Greg
#2

[eluser]jairoh_[/eluser]
post code please. Smile
#3

[eluser]Tekamba[/eluser]
------------------------------------------------------
views/contact.php (excerpt)
------------------------------------------------------
Code:
<div id="contact-form">
      <h2>Contact Us</h2>
      &lt;?php
      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();
      ?&gt;
    </div> &lt;!-- end contact-form --&gt;

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

    // load the form validation library
    $this->load->library('form_validation');

    //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);
      $this->email->set_newline("\r\n");

// set to, from, message, etc
      $this->email->from($this->input->post('email'));
      $this->email->to($config['recipient']);
      $this->email->subject($msg_subject);
      $this->email->message($messagebody);

// 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
      $this->load->view('includes/template');
    } else {
      // form failed to validate so pass the errors
      // to flashdata and then return user to form
      $this->session->set_flashdata('errors', validation_errors());
      $this->load->view('includes/template');
    }
  }

------------------------------------------------------
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
------------------------------------------------------
Code:
&lt;?php
$this->load->view('includes/startpage');
$this->load->view('includes/header');
?&gt;

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

&lt;?php
$this->load->view('includes/footer');
$this->load->view('includes/endpage');
?&gt;
#4

[eluser]jairoh_[/eluser]
there is nothing wrong w/ the form. Smile

just try to fresh re-install CI.




Theme © iAndrew 2016 - Forum software by © MyBB