Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/478.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
防止重复条目-Ajax不返回错误。PHP MySQL jQuery_Php_Javascript_Jquery_Mysql - Fatal编程技术网

防止重复条目-Ajax不返回错误。PHP MySQL jQuery

防止重复条目-Ajax不返回错误。PHP MySQL jQuery,php,javascript,jquery,mysql,Php,Javascript,Jquery,Mysql,我正试图防止在我的数据库中为时事通讯提交表单复制实体。我成功地阻止了重复条目,但是我无法修改jQuery以打印错误。我已经包括了PHP,以防我遗漏了一些东西。。。任何帮助都将不胜感激 jQuery: submitHandler: function(form) { jQuery(form).ajaxSubmit({ success: function() { $('#newsletterForm').html("<div

我正试图防止在我的数据库中为时事通讯提交表单复制实体。我成功地阻止了重复条目,但是我无法修改jQuery以打印错误。我已经包括了PHP,以防我遗漏了一些东西。。。任何帮助都将不胜感激

jQuery:

submitHandler: function(form) {
        jQuery(form).ajaxSubmit({
            success: function() {
                $('#newsletterForm').html("<div id='success-message'></div>");
                $('#success-message').html("<p><strong>Thank you.</strong> <br/>You've been added to our list and will hear from us soon!</p>");
            },
            error: function() {
                $('#newsletterForm').html("<div id='success-message'></div>");
                $('#success-message').html("<p><strong>That email already exists.</strong> <br/>Please enter another email address.</p>");
            }
        });
    }

你为什么不能打印出一个JSON来表示发生了什么,而不是回显消息呢?这将允许您在JavaScript中直接操作对象


请记住:jQuery不是一种语言。

只有在AJAX请求失败(例如404)时才会调用该错误

success:function(){}
中,您需要确定PHP脚本返回的是成功消息还是错误消息。最简单的方法是使用JSON而不是纯文本

为此,在PHP中,不要回显字符串,而是执行

echo json_encode(array('result'=>'success','response'=>'Everything worked');
或者


然后在ajax中,将数据类型设置为“json”,然后在成功函数中,您可以将json作为对象访问。

谢谢您的回复。我试试看
echo json_encode(array('result'=>'success','response'=>'Everything worked');
echo json_encode(array('result'=>'error','response'=>'Already exists');