简体   繁体   中英

How can I give an error if all fields are not filled in?

Basically this is my registration form. I want to make it so if all the fields are not filled in it will give an error. What do I do from here? I would also like to display the error as $msg

<?php
include'db.php';
$msg='';
if (empty($_POST[''])
|| empty($_POST['password'])
|| empty($_POST['repassword'])
|| empty($_POST['user_firstname'])
|| empty($_POST['user_lastname'])
){
// details sent Form
$company=mysql_real_escape_string($_POST['company']);
$address=mysql_real_escape_string($_POST['address']);
$email=mysql_real_escape_string($_POST['email']);
$phone=mysql_real_escape_string($_POST['phone']);
$regex = '/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/';        //this defines what a valid email should be

if(preg_match($regex, $email))
{
$activation=md5($email.time()); // Encrypted email+timestamp, so randomly generated and   unique

$count=mysql_query("SELECT uid FROM preciousmetals WHERE email='$email'") or  die(mysql_error());
if(mysql_num_rows($count) < 1)
{
mysql_query("INSERT INTO preciousmetals(company,address,email,phone,activation)    VALUES('$company','$address','$email','$phone','$activation');");



// sending email

include 'smtp/Send_Mail.php';
$to=$email;
$subject="Email verification";
$body='Hello, we need to make sure you are human. Please verify your email and get   started using your Website account. '.$base_url.''.$activation.'';

Mail($to,$subject,$body);

$msg= "Registration successful, please activate email.";    


}
else
{
$msg= '<font color="#cc0000">This email is already in use, please enter a different one.</font>';   
}

}
else
{
$msg = '<font color="#cc0000">The email you have entered is invalid, please try again. </font>';  
}


}
?>

Thank you for helping out!

Simply do

if (empty($_POST['password']) || empty($_POST['repassword']) || empty($_POST['user_firstname']) || empty($_POST['user_lastname'])){
    $msg = 'You have not filled all fiels';
} else {
    // details sent Form
    ......
}
echo $msg;

And also check for filter_var() for email validation its much more nicer than regEx http://php.net/manual/en/filter.examples.validation.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM