using $_post in function

#1
[eluser]trevor2[/eluser]
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;
            }
        }

#2
[eluser]trevor2[/eluser]
I think I just saw an unrelated issue with checking the user. Working on it.
$str isn't doing anything.

#3
[eluser]urrus[/eluser]
Hallo

I think the problem in function definition function username_check($_POST['username']);

first of all you do not need this (Wink at the end of the string
the second look at the your functions arguments, in definition there must be names of variables you should use in work of you function, note existed ones, it's like a white spots you will fill it when you call this function. The meaning of existing arguments in functions: you can put different data in the same function.

So spend some time to read about functions in Internet, it's important thing.

And the last one, that how it should be write:
Code:
function username_check($str)
        {
///----------------------------->

/*and when calling function:*/

username_check($_POST['username']);

#4
[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:

Code:
validation_errors()

#5
[eluser]trevor2[/eluser]
For some reason I keep getting my error message from the username_check function. Regardless if the name is in the database.
Code:
function username_check($username)
        {
            $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;
            }
        }

If I substitute this, it will work.
Code:
if(!$query->num_rows > 0)

#6
[eluser]skunkbad[/eluser]
It looks like your query is set up wrong, or at least I don't do it the way you do. Check the query out of the context of the callback, and make sure it works.

#7
[eluser]trevor2[/eluser]
This works, thanks everyone.
Code:
function username_check($username)
        {
            $this->load->database();
            
            $query = $this->db->query("SELECT username FROM ci_members WHERE username = '$username'");
            
            if($query->num_rows() > 0)
            {
                $this->form_validation->set_message('username_check', 'Please choose another name');
                return FALSE;
            }
            else
            {
                return $username;
            }
        }


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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