• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem with Validation when using an image submit button

#1
[eluser]Oussama M Billah[/eluser]
Hello,

I m having a form with an image submit button.

When filling the form and clicking the submit button the form is correctly validated.
However, when i fill the form and press the enter button the form is not validated and the same page is reloaded with no error msg.

when I use a plain submit button ( no image ) everything works fine.

the only difference between the two type of submit buttons is that when using an image
I add '_x' to the submit button name in the controller.
What can be the problem that causes the form to not validate when pressing the enter button.

Thanks

#2
[eluser]pistolPete[/eluser]
Post your code please.

#3
[eluser]Zeeshan Rasool[/eluser]
it can found if some one check you code,

#4
[eluser]Oussama M Billah[/eluser]
here is the controller :

Code:
function index(){
        $this->load->library(array('form_validation', 'session'));
            
            // LOAD HELPERS
            $this->load->helper(array('form', 'url'));
            
            // SET VALIDATION RULES
            $this->form_validation->set_message('required', 'The %s field is required. <br/>');
            $this->form_validation->set_message('valid_email', 'The %s field must contain a valid email address. <br/>');
            $this->form_validation->set_message('min_length', 'The %s field must be at least 6 characters in length. <br/>');
            $this->form_validation->set_message('matches', 'The %s field does not match the password field. <br/>');
            
            
            $this->form_validation->set_rules('userName', 'username', 'required');
            $this->form_validation->set_rules('password', 'password', 'required|min_length[6]|');
            $this->form_validation->set_rules('confirmPassword', 'confirm Password', 'required|matches[password]');
            $this->form_validation->set_rules('firstName', 'first name', 'required');
            $this->form_validation->set_rules('lastName', 'last name', 'required');
            $this->form_validation->set_rules('email', 'email address', 'required|valid_email');
    
            $this->form_validation->set_error_delimiters('<em>','</em>');
            
            // has the form been submitted and with valid form info (not empty values)
            if($this->input->post('register_x'))
            {
                if($this->form_validation->run())
                {    
                    $userName = $this->input->post('userName');
                    $user = $this->CustomerModel->getCustomer($userName);
                    if($user->getUserName()){
                        $this->session->set_flashdata('message', 'user already exists! Try again');
                        redirect('register');
                    }

                    $password = $this->input->post('password');
                    $confirmPass = $this->input->post('confirmPassword');
                    $fname = $this->input->post('firstName');
                    $lname = $this->input->post('lastName');
                    $email = $this->input->post('email');
    
                    $customer = new Customer();
                    $customer->setUserName($userName);
                    $customer->setPassword($password);
                    $customer->setFirstName($fname);
                    $customer->setLastName($lname);
                    $customer->setEmailAddress($email);
                        $this->CustomerModel->register($customer);
                        $this->session->set_userdata('user', $userName);
                        return redirect('products');        
                }
            }
            $this->load->view('register.php');                    
            
    }


here is the form code in my view:

Code:
<div class="register_error">
    &lt;?php
      $this->load->library('form_validation');
      if($this->session->flashdata('message')){
       echo $this->session->flashdata('message');
       }
        echo form_error('userName');
           echo form_error('password');
        echo form_error('confirmPassword');
        echo form_error('firstName');
        echo form_error('lastName');
        echo form_error('email');
       ?&gt;
    </div>
    &lt;form name="register" action="register" method="post"&gt;
        User Name:<br />
            &lt;input align="right" size="30" name="userName"/&gt;
          Password:<br />
          &lt;input type="password" size="30" name="password" /&gt;
          Confirm Password:<br />
          &lt;input type="password" size="30" name="confirmPassword" /&gt;
First Name:<br />
          &lt;input size="30" name="firstName" /&gt;
          Last Name:<span style="color:#FF0000">* </span><br />
          &lt;input size="30" name="lastName" /&gt;
          Email Address:<span style="color:#FF0000">* </span><br/>
          &lt;input size="35" name="email"/&gt;
          <br />
           &lt;input type="image" name= "register" src="images/register.jpg" alt="register" width="148" height="34" /&gt;&lt;/div>
&lt;/form&gt;
        </div>

#5
[eluser]Aken[/eluser]
1) Your problem is that when you hit the enter key, the name value of the image does not transfer in with the rest of the form variables.

2) You should not check for a post variable to see if the form is submitted. The form_validation->run() function does that for you. Read the form_validation page carefully, it explains how to check for that very thing.

3) You don't need to load the form_validation library again in your view. Once in the controller is sufficient.

4) You can use custom functions called callbacks as a rule when validating a form. You should consider using that for checking if the username exists instead of using flashdata and redirects.

5) When doing redirects, you don't use the return property with them. You should also use the exit; property right after a redirect.

Your code seems to work, but it can be cleaned up a lot. Those are good places to start.

#6
[eluser]Oussama M Billah[/eluser]
Thanks Aken,

This is my first application in php & codeIgniter so the code may not be perfectly clean.

Will fix the small problems based on your suggestions.

Thanks again.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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