07-31-2008, 04:30 AM
[eluser]Michael Wales[/eluser]
Place a unique identifier within each of the forms (I like to use the submit button).
Place a unique identifier within each of the forms (I like to use the submit button).
Code:
<?php echo form_open('process'); ?>
<input type="text" name="q" />
<input type="submit" name="btnSearch" value="Search" />
<?php echo form_close(); ?>
<?php echo form_open('process'); ?>
<input type="text" name="username" />
<input type="password" name="password" />
<input type="submit" name="btnLogin" value="Login" />
<?php echo form_close(); ?>
Code:
function process() {
$this->load->validation();
// This is one approach - personally, I would use the if...then to determine the form
// that was submitted and then call a private method to handle that POST request
if ($this->input->post('btnSearch')) {
$rules = array('q' => 'trim');
$fields = array('q' => 'Search terms');
} elseif ($this->input->post('btnLogin')) {
$rules = array('username' => 'required|trim|callback__check_login',
'password' => 'required');
$fields = array('username' => 'username',
'password' => 'password');
}
$this->validation->set_rules($rules);
$this->validation->set_fields($fields);
if ($this->validation->run()) {
// Do some stuff, based on the type of form that was submitted
}
}