如何进行Ajax调用

时间:2017-06-23 13:08:05

标签: php ajax wordpress

我有一个表单只是为了向wp_wpa_watchlist的自定义表格提交电子邮件。

Form html是:

<form name="anyreg" id="any_reg" method="post">
                    <table>
                    <tr>
<td><input type="text" name="email" id="mail" placeholder="Enter Your Email"></td>
            <input type="hidden" name="id" value="<?php echo $auction ?>">
            <td><input type="submit" name="submitmail" id="submitmail" value="Submit"></td>
            </tr>

验证码+ ajax呼叫代码是:

<script type="text/javascript">
$('#submitmail').click(function(event){

            event.preventDefault();

            var tre = true;


 var anymail = document.getElementById("mail").value;
            var atpos = anymail.indexOf("@");
            var dotpos = anymail.lastIndexOf(".");

                if (anymail==null || anymail=="")
                    {  
                    alert("Email cannot be Empty");
                    tre =false;  
                    }

                else if (atpos<1 || dotpos<atpos+2 || dotpos+2>=anymail.length) 
                    {
                    alert("Please Enter A Valid Email Address");
                    tre =false;
                     }
            if(tre == true){
        $.ajax({url: '<?php echo $ajaxUrl; ?>',method:'POST',
                data:$('#any_reg').serialize()
                ,success: function(result){
                    if (result== 'INSERTED'){
                        alert("SUCCESS");   
                    }else
                    {
                        alert("Failed");
                    }

                },

        });
}
});
</script>

我的ajax调用文件包含以下查询:

<?php 
$parse_uri = explode( 'wp-content', $_SERVER['SCRIPT_FILENAME'] );
require_once( $parse_uri[0] . 'wp-load.php' );

global $wpdb;

$email=$_POST['email'];
$auction=intval($_GET['ID']);

if(!email_exists( $email ))
{
        $insert = "INSERT INTO wp_wpa_watchlist (auction_id,watch_email) VALUES('".$auction."', '".$email."')";
        $dbin = $wpdb -> query($insert);    

        if($dbin){
            echo "INSERTED";
        }
        else{
            echo "ERROR";
        }

}

else if 
    { 
        echo "Email Already Exists";
    }

?>
当谈到ajax和ajax调用时,我非常新手。我的问题是ajax调用没有发生。当我检查控制台时显示内部错误500。 任何人请指导我

0 个答案:

没有答案