Returning a value from a function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jameso
Forum Newbie
Posts: 7
Joined: Wed Mar 17, 2004 3:31 pm

Returning a value from a function

Post 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
Steveo31
Forum Contributor
Posts: 416
Joined: Sun Nov 23, 2003 9:05 pm
Location: San Jose CA

Post by Steveo31 »

So.... whats the problem? :)
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post 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.
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post 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:
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post 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.
Steveo31
Forum Contributor
Posts: 416
Joined: Sun Nov 23, 2003 9:05 pm
Location: San Jose CA

Post 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 
?>
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

I only copied what was in his post.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Sami wrote:I only copied what was in his post.
Well then you're a bad, bad person :x :P

Mac
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

:twisted:
Post Reply