-1

I have the following code to validate form data. I have created functions to validate various groups, and then have an if isset statement to check if these functions return true. I have tried many different ways to get this to work.

The problem I am having is this. I want the if isset to end if returning FALSE; but it doesn't, it keeps going and pops up the next alert (in my code I have many functions). How can I get it to exit after the first return FALSE? Do I need to make the isset into a function? So it can exit on return FALSE. thanks

I am having trouble writing a function to call functions in php.

function namecheck ($fname, $lname) 
{
    $regexp ="/^[A-Za-z]+$/";
    //filter through names 
    if (preg_match($regexp,$fname,$lname)) 
    {
        return TRUE; 
    }
    else 
    {
        echo'<script type="text/javascript">alert("Enter your names.")</script>';
        return FALSE; 
    }
}

function emailcheck ($email1, $email2) 
{
    $regexp="/^[a-zA-A-Z0-9_.]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9.-]+$/";
    //validate email address    
    if (preg_match($regexp,$email1,$email2)) 
    {
        return TRUE; 
    }
    else 
    {
        echo '<script type="text/javascript">alert ("Enter a valid email address.")</script>';
        return FALSE; 
    }
}

$fname=$_POST['fname'];
$lname=$_POST['lname'];
$namecheck=namecheck($fname,$lname);
$email1=$_POST['email1'];
$email2=$_POST['email2'];
$emailcheck=emailcheck($email1,$email2);

if (isset($_POST['submit'])) 
{
    if ($namecheck !==TRUE)
    {
        return FALSE;
    }
    elseif ($emailcheck!==TRUE)
    {
        return FALSE;
    } //and so on..
    else
    {
        return TRUE;
    }
}
  • your code is almost impossible to read. how can you tell you have the right number of curly braces? – user428517 Aug 02 '13 at 17:15
  • You need to improve the way you write your code. sgroves is right. You your self would not be able to understand it after some days. –  Aug 02 '13 at 17:23
  • Return false or true does nothing when not in a function scope. – Jeremy Harris Aug 02 '13 at 17:24
  • see my answer @skippy. not lining up your curly braces with the start of their blocks makes it VERY difficult to tell if you've balanced them properly. if your code runs, it means there is balance, but that doesn't mean the code necessarily does what you want it to. well-formatted code is essential to effective debugging. – user428517 Aug 02 '13 at 17:26
  • put it in a function, then call the function? – user428517 Aug 02 '13 at 17:30
  • @skippy php is no different from other languages in terms of structure. what exactly do you want to **do** with this code? where you write `return FALSE` in your code, where do you intend for that to take you? what purpose is it intended to serve? it's hard to help without knowing your intentions. – user428517 Aug 02 '13 at 17:39
  • 1
    @Skippy My answer should clear things up for you. – Jeremy Harris Aug 02 '13 at 17:48

3 Answers3

2

If you want the script to, as you put, "exit" then you need to use exit(); Generally this is bad as the script will completely stop executing. Maybe you can look into using "break;" to get you out of a loop and stop executing functions within that loop. Another problem is that you are echoing out HTML code in your function which gets executed on assignment and so you will always get an alert generated when it evaluates to FALSE.

edit: within your if(isset()) block. Inside here you can do{}while(false); which is a loop and will let you break out of it at anytime and prevent further execution of code within that loop.

Derek Wright
  • 1,452
  • 9
  • 11
  • `break` would do nothing here. you can't `break` out of an `if` block; that doesn't make any sense. – user428517 Aug 02 '13 at 17:24
  • Yea ignore my last part about the breaking out of an if - brain fart. The problem, I think, he is talking about is the alerts keep happening. Which are going to continue to happen because his functions execute on assignment of the $namecheck,$emailcheck variables. To be honest - I hate mixing display code in with logic code. – Derek Wright Aug 02 '13 at 17:32
  • I would do this, get rid of those functions as they dont add much value to the flow/logic of your code. You can directly test the return values of the preg_match. within your if(isset()) block. Inside here you can do{}while(false); which is a loop and will let you break out of it at anytime and prevent further execution of code within that loop. – Derek Wright Aug 02 '13 at 17:44
  • 1
    Added the do/while loop to my answer – Derek Wright Aug 02 '13 at 17:50
2

If a function isn't returning false, then it never reached a return FALSE; statement. It's as simple as that. So let's examine the relevant code:

if (isset($_POST['submit'])) 
{
    if ($namecheck !==TRUE)
    {
        return FALSE;
    }
    elseif ($emailcheck !== TRUE)
    {
        return FALSE;
    } //and so on..
    else
    {
        return TRUE;
    }
}

So, if $_POST['submit'] is set and is not null, the if block will be reached. Then, if $namecheck is not true OR $emailcheck is not true, the function will return FALSE. You can simplify the above code to just:

if (isset($_POST['submit'])) 
{
    return !(!$namecheck || !$emailcheck);
}

However, it doesn't look like this code is inside a function, so the return statement will do nothing. You have to put it in a function if you want it to work like a function.

Beyond that, I can't help you. I don't know what you want to do with this code. You seem to know how to use and call functions, so I'm not sure what the problem is. If you want to return from a function, put code in a function and call return. Right now your code is not in a function, so return won't do anything.

user229044
  • 232,980
  • 40
  • 330
  • 338
user428517
  • 4,132
  • 1
  • 22
  • 39
2

A general structure for your functions you could follow is something like this:

function validateName($name) {
   // Do Validation. Return true or false.
}

function validateEmail($email) {
   // Do Validation. Return true or false.
}

function isFormValid() 
{
   // Name Validation
   if( ! validateName( $_POST['name'] ) )
      return false;

   // Email Validation
   if( ! validateEmail( $_POST['email'] ) )
      return false;

   // Form is valid if it reached this far.
   return true;
}

// In your regular code on Form Submit
if( isset($_POST['submit']) )
{
   if( isFormValid() ) {
      // Save Form Data to DB
   } else {
      // Show Some Errors
   } 

}

That general structure should work fine for you. It could be made a LOT better but, for the sake of learning, this is sufficient.

Jeremy Harris
  • 24,318
  • 13
  • 79
  • 133
  • 1
    @skippy you can call as many functions you want from inside a function. to create a function that calls other functions, you just ... *write a function that calls other functions*. this is true of practically every language with functions. – user428517 Aug 02 '13 at 17:54
  • 1
    @skippy sorry, reviewing other's code is not a good use of my time. you seem to be figuring it out though. – user428517 Aug 02 '13 at 18:03