简体   繁体   中英

PHP Code Error. Can't find my logic mistake

Ok. So this is my code below. I'm trying to follow a tutorial on webtuts about validating email. But my sample is not working out. It is supposed to alert the user that it has entered an invalid email. So what my mate did is he created the "show_warning" jquery function to allow me to display my $msg. But it doesn't work. Is my logic wrong?.

<?php

if(isset($_POST['username']) && !empty($_POST['username']) AND
   isset($_POST['password']) && !empty($_POST['password']) AND
   isset($_POST['email']) && !empty($_POST['email']) AND
   isset($_POST['role_id']) && !empty($_POST['role_id'])) 
    {
        $username = ($_POST['username']);
        $password = ($_POST['password']);
        $email = ($_POST['email']);
        $role_id = ($_POST['role_id']);  

            if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email))
            {  
                $msg = 'The email you entered is invalid. Please try again.';
            }
                else
            {  
                $msg = 'Your account has been made, <br /> please verify by clicking the activation link in your email.';  
            }     
    } 
?>

======================================

<div id="main-content">
  <div id="create-user">

      <h1>Create User</h1>
      <form action="" method="post">
      <table id="userform" width="600px" border=0>
        <tr>
          <td><label for="username">Username</label>
            <input type="text" id="username" name="username" /></td>
          <td><label for="password">Password</label>
            <input type="text" id="password" name="password" /></td>
        </tr>
        <tr>
          <td><label for="email">Email</label>
            <input type="text" id="email" name="email" /></td>
          <td><label for="role_id">Role</label>
            <select>
              <?php $roles = load_roles() ;?>
              <?php foreach ($roles as $role): ?>
              <option value='<?php echo $role['role_id']; ?>'><?php echo $role['role']; ?></option>
              <?php endforeach; ?>
            </select></td>
        </tr>
        <tr>
          <td><input type="submit" id="submit" name="save_user" value="Create User"></td>
        </tr>
      </table>
    </form>
  </div>
</div>

for validating email php provides

$email="test@gmail.com" //your email to validate here

if(filter_var($email, FILTER_VALIDATE_EMAIL)){

       echo "E-mail is valid";
}
else
{
      echo "E-mail is not valid";
}

and you must not use eregi . you can use preg_match()

for more validation function follow this link

http://php.net/manual/en/filter.filters.validate.php

eregi() function has been DEPRECATED as of PHP 5.3.0. Relying on this feature is highly discouraged.

so its would be actual reason

AND preg_match() with the i (PCRE_CASELESS) modifier is the suggested alternative.

as in answer of Yadav Chetan use FILTER_VALIDATE_EMAIL instead those regx

 if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email))
            {  
                echo $msg = 'The email you entered is invalid. Please try again.';
            }
                else
            {  
                echo $msg = 'Your account has been made, <br /> please verify by clicking the activation link in your email.';  
            }   

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