At the moment I am trying to validate a form using PHP. The problem is, that even after entering something wrong, PHP interprets it as right. Obviously I don't now why, although I have an assumption. This is the code:
if(isset($_GET['contact'])){
// Validation functions
// Name
function validate_name(){
$name = $_POST['customer'];
if(strlen($name) > 0){
trim(mysql_real_escape_string($name));
return true;
}else {
return false;
}
}
// Mail
function validate_mail(){
$mail = $_POST['mail'];
if(preg_match('/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/', $mail) && strlen($mail) > 0){
return true;
}else {
return false;
}
}
// Message
function validate_message(){
$message = $_POST['message'];
if(strlen($message) > 0){
trim(mysql_real_escape_string($message));
return true;
}else {
return false;
}
}
validate_name();
validate_mail();
validate_message();
if(validate_name == true && validate_mail == true && validate_message == true){
echo "Ok!";
}else{
echo "Error!";
}
}
One thing I know is bad is this: if(validate_name == true && validate_mail == true && validate_message == true){}
.
But if I am not mistaken, this still works because PHP can handle something like this (PHP only gives a notice, not an error). But how to do it right, there must be a better way?
The second this I found out is, that PHP basically calls the functions correct, but inside the functions the if-else is not working. Why? I don't understand this...