[eluser]skunkbad[/eluser]
[quote author="trevor2" date="1250413299"]I can't seem to get this going for some reason.
My error on "line 14" is where the function is declared.
Parse error: parse error, expecting `')'' in C:\xampp\htdocs\CodeIgniter\system\application\controllers\main.php on line 14
Here is the code
Code:
function username_check($_POST['username']); // Line 14
{
$str = $_POST['username'];
$this->load->database();
$query = $this->db->query('SELECT username FROM ci_members');
if($query->num_rows > 0)
{
$this->form_validation->set_message('username_check', 'Please choose another name');
return FALSE;
}
else
{
return TRUE;
}
}
[/quote]
When using the form validation class, you aren't going to need to directly access the $_POST array. You are also not setting your validation rule up correctly. When you set your validation rule, the first argument is the name of the $_POST array element that you want to check. So in this case, you would set your rule like this:
Code:
$this->form_validation->set_rules('username', '', 'callback_username_check');
Then your callback function would be something like this:
Code:
function username_check($username); // Line 14
{
$this->load->database();
$this->db->where('username',$username);
$query = $this->db->query('SELECT username FROM ci_members');
if($query->num_rows > 0)
{
$this->form_validation->set_message('username_check', 'Please choose another name');
return FALSE;
}
else
{
return $username;
}
}
When or if the username actually passes validation, it will be available as:
Code:
set_value('username')
If the username does not pass validation, the error will be included in the output of: