使用Javascript/Ajax运行php脚本


Using Javascript / Ajax to run a php script

我在表单底部有一个按钮,如果用户尝试使用的电子邮件已经在使用,我会尝试禁用它。用户提供的电子邮件是$email

这是我的前端代码。。

        <script type="text/JavaScript">
        var email = '<?php echo $email; ?>';//Get the value in the username textbox
        $.ajax({  //Make the Ajax Request
            type: "POST",  
            url: "ajax_check_email.php",  //file name
            data: "email="+ email,  //data
            success: function(server_response){  
           $("#availability_status").ajaxComplete(function(event, request){ 
            if(server_response == '0')//if ajax_check_username.php return value "0"
            { 
             alert("Server response recieved: 0");
            $("#availability_status").html('<img src="/img/icon-success.png" align="absmiddle" style="height: 15px; width: auto; margin-top: -2px;"> <font color="#c9dc54" style="padding-top: 5px;"> Available </font>  ');
            $("#btn-submit-2").html('<button type="submit"  class="btn btn-danger btn-lg btn-block"   id="b"  style="font-family: "klavikaRegular"; letter-spacing: 1px;" name="skip" value="0">CONTINUE</button>');
            //add this image to the span with id "availability_status"
            }  
            else if(server_response == '1')//if it returns "1"
            {  
             alert("Server response recieved: 1");
             $("#availability_status").html('<img src="/img/icon-error.png" align="absmiddle" style="height: 15px; width: auto; margin-top: -2px;"> <font color="#b11116" style="padding-top: 5px;">Not Available </font>');
             $("#btn-submit-2").html('<a class="btn btn-default btn-lg btn-block" style="font-family: "klavikaRegular"; letter-spacing: 1px;">CONTINUE</a>');
            }  
           });
           } 
          }); 
        }
        else
        {
        alert("failed query");
        $("#availability_status").html('<font color="#b11116">Username too short</font>');
        $("#btn-submit").html('<a class="btn btn-default btn-lg btn-block" style="font-family: "klavikaRegular"; letter-spacing: 1px;">USERNAME TOO SHORT</a>');
        //if in case the username is less than or equal 3 characters only 
        }
        </script>

这是我的php文件源

    <?php
    include('database_connection.php');
    //Include The Database Connection File 
    if(isset($_POST['email']))//If a username has been submitted 
    {
    $email = mysql_real_escape_string($_POST['email']);//Some clean up :)
    $check_for_email = mysql_query("SELECT user_login FROM wp_users WHERE user_email='$email'");
    //Query to check if username is available or not 
    if(mysql_num_rows($check_for_email))
    {
    echo '1';//If there is a  record match in the Database - Not Available
    }
    else
    {
    echo '0';//No Record Found - Username is available 
    }
    }
    ?>

我的javascript语法有问题吗?

$email被硬编码为我知道应该失败的东西。。

尝试在php页面中成功写入:

显示页面

<div id="availability_status"></div>
<script type="text/JavaScript">
    var email = '<?php echo $email; ?>';//Get the value in the username textbox
    $(document).ready(function() {
            $.ajax({  //Make the Ajax Request
                    url: "ajax_check_email.php?email="+email,  //data
                    cache: false,
                    success: function(server_response){
                            $("#availability_status").html(server_response);
                        }
                });
        });
</script>

**ajax_check_email.php**

<?php
    include('database_connection.php');
    //Include The Database Connection File 
    if(isset($_GET['email'])) {
            if(filter_var($_GET['email'], FILTER_VALIDATE_EMAIL)) {
                    $email              =   mysql_real_escape_string($_POST['email']);//Some clean up :)
                    $check_for_email    =   mysql_query("SELECT user_login FROM wp_users WHERE user_email='$email'");
                    //Query to check if username is available or not 
                    if(mysql_num_rows($check_for_email)) { ?>
        <img src="/img/icon-error.png" align="absmiddle" style="height: 15px; width: auto; margin-top: -2px;">
        <font color="#b11116" style="padding-top: 5px;">Not Available </font>
                <?php   }
                    else { ?>
        <img src="/img/icon-success.png" align="absmiddle" style="height: 15px; width: auto; margin-top: -2px;"> <font color="#c9dc54" style="padding-top: 5px;"> Available </font>
        <button type="submit"  class="btn btn-danger btn-lg btn-block"   id="b"  style="font-family: "klavikaRegular"; letter-spacing: 1px;" name="skip" value="0">CONTINUE</button>
                <?php   }
                }
            else { ?>
            <h2>Invalid Email Address.</h2>
            <?php }
        } ?>

是的,您的JavaScript中存在语法错误。

    }
    else
    {
    alert("failed query");
    $("#availability_status").html('<font color="#b11116">Username too short</font>');
    $("#btn-submit").html('<a class="btn btn-default btn-lg btn-block" style="font-family: "klavikaRegular"; letter-spacing: 1px;">USERNAME TOO SHORT</a>');
    //if in case the username is less than or equal 3 characters only 
    }

那里的括号真的没有关闭任何东西,而且对于else没有if(如果)(唯一一个真正在$.ajaxComplete()调用中)。

你不需要像那样使用$.ajaxComplete。

$.ajax({success: function(data){
       if(data == '1') {
        //If server returns 1 do...
       } else if(data == '0') {
         //If server returns 0 do...
       } else {
         //Nothing returned
       }
     }
});

另外,将AJAX的dataType指定为文本。

$.ajax({dataType: 'text',
        success: ... });