JQuery(JavaScript)+ PHP语法解释

时间:2017-06-04 21:58:22

标签: javascript php jquery

进行一些练习,包括JQuery + PHP结合在一起。我不完全理解的部分是在if语句启动的Jquery代码中,有人可以从这一点解释发生了什么吗?

HTML code:

<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <form action="" method="post"> 
            <label for="name">Name:</label><br> 
            <input type="text" name="name"><br><br> 
            <label for="email">Email:</label><br> 
            <input type="text" name="email" id="email" autocomplete="off"><br><br> 
            <label for="password">Password:</label><br> 
            <input type="text" name="password"><br><br> 
            <input type ="submit" name="submit" value="Sign up"> 
            <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
            <script src="script.js" type="text/javascript"></script>
    </body>
</html>

PHP代码:

<?php

if(! empty($_GET['email'])){ 

    $email=filter_var($_GET['email'],FILTER_VALIDATE_EMAIL); 

    if($email){ 

        $con="mysql:host=localhost;dbname=eshop;charset=utf8"; 
        $db= new PDO($con,'root',''); 
        $query=$db->prepare("SELECT email FROM users WHERE email = ?"); 
        $query->execute([$email]); 
        $email=$query->fetch(PDO::FETCH_ASSOC); 

        if($email){ 

            echo true; 
        }
    }
}

JQuery代码:

$('#email').on('keyup', function(){ 

    var userEmail= $(this).val().trim(); 
    $('#result').remove(); 
    var emailRegex= /^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(\.[a-z]{2,3})$/i; 

    if(emailRegex.test(userEmail)){ 
        $.get('check_email.php',{email:userEmail},function(res){ 

            if(res ==1){ 

                $('#email').after('<span id="result">*Email is taken</span>');
            }
        });
    }
});

0 个答案:

没有答案