0

I am new to PHP, so I apologize if this looks like a mess... I am trying to validate a form using the following three functions - checkName, checkEmail, and checkMessage. The problem I am running into is when I submit the form, it always displays the first error, even if the input is correct. Can anyone tell me what I'm doing wrong?

function checkName(){

    if($name == ''){
        print "Please enter your name!<br />";
        return false;
    }
    else{
        if(strlen($name)<2) {
            print "Your name should be more than 1 characters long!<br />";
            return false;
        }
        else{
            return true;
        }
    }
}
function checkEmail(){

    if($from == '') {
        print "Please enter your email address!<br />";
        return false;
    } 
    else{
        if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$", $from)){
            print "Please enter a valid email address!<br />";
            return false;
        }
        else{
            return true;
        }
    }
}
function checkMessage(){

    if($message == '') {
        print "Please enter your message!<br />";
        return false;
    }
    else{
        if(strlen($message)<10) {
            print "Your message should be more than 10 characters long!<br />";
            return false;
        }
        else{
            return true;
        }
    }
}

if($validation == ''){

    $a = checkName();
    $b  =  checkEmail();
    $c = checkMessage();

    $result = array($a, $b, $c);

    return $result;
1
  • 2
    "PHP Return Multiple Functions"? Commented Jul 8, 2012 at 20:42

2 Answers 2

3

Pass the variables to test into your functions to check them. The way you have it now, it would assume you are using global variables for $name,$message,$email. That would require the use of the global keyword (or some other options) in the functions, but is considered poor practice. Best to pass the variables

Called as:

$a = checkName($name);
$b  =  checkEmail($email);
$c = checkMessage($message);

Definitions

// Pass variable to function
function checkName($name){

    if($name == ''){
        print "Please enter your name!<br />";
        return false;
    }
    else{
        if(strlen($name)<2) {
            print "Your name should be more than 1 characters long!<br />";
            return false;
        }
        else{
            return true;
        }
    }
}
function checkEmail($email){
  // etc...
}
function checkMessage($message){
  // etc...    
}

By the way, as someone who frequently has to maintain old PHP code written by others, I can tell you that it is highly recommended that you do not use variable names like $a,$b,$c. Instead make them readable like $nameResult, $emailResult, $messgeResult.

Sign up to request clarification or add additional context in comments.

Comments

0

In the functions your variables are not defined. If they are defined at all you have to use global $variable in your functions to have them defined in your functions

example: bad:

$var = 'Hello';
function fun () {return $var;}
echo fun () . ' world';

good:

$var = 'Hello';
function fun () {
    global $var;
    return $var;
}
echo fun () . ' world';

1 Comment

I have them defined in the actual script. Part of this script is for a company, and I don't want to post the personal information. I know i have the variables defined correctly, I just don't know how to properly return the functions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.