Question

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;
    }
}
Was it helpful?

Solution

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.

OTHER TIPS

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top