Welcome Guest, Not a member yet? Register   Sign In
Validation
#1

[eluser]Zombie1[/eluser]
Hello all,

I'm a newbie to CodeIgniter. I'm having some trouble with my form validation. My form allows a user to enter a username and password then checks it against a database. If the username or password do not match the program displays an error message. If the username and password do match then they are redirected to a page called my_blog.

Here's the code for the controller:

Code:
class Form extends CI_Controller {

function index()
{

  $this->load->helper(array('form', 'url'));

  $this->load->library('form_validation');
  
  $this->form_validation->set_rules('username', 'Username', 'required');
  $this->form_validation->set_rules('password', 'Password', 'required');


  if ($this->form_validation->run() == FALSE)
  {
   $this->load->view('log_in');
  }
  else
  {
   $this->load->view('my_blog');
  }
}

function user_not_exists($username) {

  $this->load->model('log_in_db');

     $user_check = $this->user_model->user_not_exists($username);

     if($user_check > 0) {
   redirect('my_blog');
   return TRUE;
     }
     else {
   $this->form_validation->set_message('required', 'Username does not match');
         return FALSE;
     }

}

function password_not_exists($password) {

     $check_password = $this->user_model->password_not_exists($password);

     if($check_password > 0) {
   redirect('my_blog');
   return TRUE;
     }
     else {
   $this->form_validation->set_message('required', 'Password does not match');
         return FALSE;
     }

}

}

Here's the code for the model which the controller links to called log_in_db:

Code:
class log_in_db extends CI_Model {

function user_not_exists($username) {

     $this->db->where('username', $username);
     $query = $this->db->get('users');

     return $query->num_rows();

}

function password_not_exists($password) {

     $this->db->where('password', $password);
     $query = $this->db->get('users');

     return $query->num_rows();

}

}

And finally here's the code for the log in form which holds the actual form including both textfields for username and password:

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
&lt;html &gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" c charset=iso-8859-1" /&gt;
&lt;title&gt;Log in form&lt;/title&gt;
&lt;link rel="stylesheet" href="../../../_css/style-sheet.css" media="screen" /&gt;
&lt;/head&gt;
&lt;body&gt;

<div id="login-form">
<div id="log-in-form">
  
   &lt;?php echo validation_errors(); ?&gt;
   &lt;?php echo form_open('my_form'); ?&gt;
    
    <table>
     <tr>
      <td class="log-in-td" align="left">
       <label for="username" class="login-text">Username</label>      
      </td>
     </tr>
     <tr>
      <td class="log-in-td" align="left">
       &lt;input name="username" type="text" size="25" maxlength="25" id="username" class="my-textfields" /&gt;                                </td>
     </tr>
     <tr>
      <td class="log-in-td" align="left">
       <label for="password" class="login-text">Password</label>      
      </td>
     </tr>
     <tr>
      <td class="log-in-td" align="left">
       &lt;input name="password" type="password" size="25" maxlength="25" id="password" class="my-textfields" /&gt;                    </td>
     </tr>
     <tr>
      <td align="center" class="log-in-td">
        &lt;input type="button" id="My-Log-in-button" value="log-in"  /&gt;
      </td>          
     </tr>
      </table>
    
   &lt;?php echo form_close(); ?&gt;
  
  </div>
</div>
&lt;/body&gt;
&lt;/html&gt;

If anyone can give me any help to why the error messages aren't displayed or the user isn't redirected when they enter a correct username and password.
#2

[eluser]CroNiX[/eluser]
Do you get the "this field is required" if you submit an empty form? According to your validation rules, that is all you are checking for:
Code:
$this->form_validation->set_rules('username', 'Username', 'required');
$this->form_validation->set_rules('password', 'Password', 'required');

You are also submitting the form to a "my_form" controller. Does this exist? It looks like the name of your controller is just "form"
Code:
&lt;?php echo form_open('my_form'); ?&gt;

I would also advise to never use "my_" for anything in your code, unless you are extending a native CI library. "my_" has a special meaning in CI and can get you in trouble unless you really understand it.




Theme © iAndrew 2016 - Forum software by © MyBB