Page 1 of 1

Returning a value from a function

Posted: Mon Mar 29, 2004 12:49 am
by jameso
We're trying to send query statements into a function and then have it output the result as another function, to be used later on on the page, where we call the row info and all that.

It's for a register page- where users have to register an ID and password for login access. If the registration is succesful then the entry is added into my MySQL database.

Here's the code:

Code: Select all

function is_valid_input ($field)
{
  echo '<br>'.$field.'<br>';
  switch ($field)
  &#123;
    case '$email' :
      if (!eregi('^&#1111;a-zA-Z0-9_\-\.]+@&#1111;a-zA-Z0-9\-]+\.&#1111;a-zA-Z0-9\-\.]+$', $email))
      &#123;
        echo 'That is not a valid email address.  Please return to the previous page and try again.';
        return true;
      &#125;
      else
        return false;
     break;
		
  case '$userID' :
    if(!eregi('^&#1111;0-9]$', $userID))
    &#123;
      echo 'That is not a valid user ID.  Please return to the previous page and try again.';
      return true;
     &#125;
    else
        return false;
     break;
  &#125;
&#125;

is_valid_input('$email');
is_valid_input('$userID');

please help! I can provide more code if needed

Posted: Mon Mar 29, 2004 9:09 am
by Steveo31
So.... whats the problem? :)

Posted: Mon Mar 29, 2004 9:13 am
by markl999
is_valid_input('$email'); .. that will pass in the literal string '$email' not the value of $email.

case '$email' :
if (!eregi('^[a-zA-Z0-9_\-\.]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$', $email))
{

That wil check if the literal string '$email' was passed in then do an eregi on $email, which is never defined or brought into scope inside the function. I can see the problem but not sure which solution you need as the code is a little unclear, but i'm guessing you just need to pull the vars you are trying to validate into scope by putting global $email, $userID; at the top of the function.

Posted: Mon Mar 29, 2004 9:17 am
by malcolmboston
i read something about this the other day

in certain circumstance you must use "$var" or $var to get the value as '$var' will just print $var

hope that didnt confuse you :roll:

Posted: Mon Mar 29, 2004 9:46 am
by m3mn0n
You should do it like this:

Code: Select all

<?php
is_valid_input('$userID', 1); // type 1 is user id checking
is_valid_input('$email', 2); // type 2 is e-mail checking
?>
Then switch between the two types instead of what you have now.

Then have them return TRUE of FALSE if they validate properly so you can place them in an if statement on the page.

Posted: Mon Mar 29, 2004 9:53 am
by Steveo31
Sami- will that get the value of $email in single quotes like that? Shouldn't it just be:

Code: Select all

<?php 
is_valid_input($userID, 1); // type 1 is user id checking 
is_valid_input($email, 2); // type 2 is e-mail checking 
?>

Posted: Mon Mar 29, 2004 3:23 pm
by m3mn0n
I only copied what was in his post.

Posted: Tue Mar 30, 2004 1:27 am
by twigletmac
Sami wrote:I only copied what was in his post.
Well then you're a bad, bad person :x :P

Mac

Posted: Tue Mar 30, 2004 3:16 am
by m3mn0n
:twisted: