简体   繁体   中英

How can I make my email code work PHP?

Hey this code is suppose to add an error message when an input was not typed on the email form and its suppose to remove the error message when you finally input the code. I have 2 of them working with the following code:

//Generate a unique code
function getUniqueCode($length = "")
{   
$code = md5(uniqid(rand(), true));
if ($length != "") return substr($code, 0, $length);
else return $code;
} 

//Generate an activation key
function generateActivationToken($gen = null)
{
do
{
    $gen = md5(uniqid(mt_rand(), false));
}
while(validateActivationToken($gen));
return $gen;
}

and this code:

//Checks if an email is valid
function isValidEmail($email)
{
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
    return true;
}
else {
    return false;
}
}

I have made my own code which puts the error messages when there are no inputs on the email form but when I input something right on the email form it does not take the error messages out of the error message box. Here is the code:

//Checks if a name is valid
function isValidName($name)
{
if (filter_var($name, MAIL_NAME_ERROR)) {
    return true;
}
else {
    return false;
}
}

So to summarize all 5 are working when there is no input like this:

  • Please enter your full name
  • Please enter a valid email address
  • Please enter your telephone number
  • Please enter your password
  • Failed security question

But only my security and email are working when I input my email address and security code like this:

  • Please enter your full name
  • Please enter your telephone number
  • Please enter your password

The name, telephone and password are not working right they only work by showing the code but they do not remove the code when I input the right information.

How can I fix my code so that all 3 will go away 1 by 1?

To make your own callback you can't just pass the constant MAIL_NAME_ERROR . If you want to pass your own function you have to do something like:

function mail_name_error(){
  //do your thing here
}
function isValidName($name){
  if(filter_var($name, FILTER_CALLBACK, array('options' => 'mail_name_error'))){
    return true;
  }
  else {
    return false;
  }
}

See the options section at http://php.net/manual/en/function.filter-var.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