0

我的 JS 和 PHP 文件都正常工作,我遇到的问题是,即使我看到 PHP 脚本导致输出“继续” ,它也总是说提交错误。数据库插入从表单中正常工作,并且计数存储了我的表中的行数。有什么我忽略的吗?

JS

var dataString = $("form").serialize();
var msg = '<?php echo $msg; ?>';

$.ajax({
    type: "POST",
    url: 'submit_form.php',
    data: dataString,
    datatype: 'html',
    error: function() {
        alert('Error');
    },
    success: function(msg) {
        if (msg == 'proceed') {
            $('#contact_form').html("<div id='message'></div>");
            $('#message').html("<h2>Contact Form Submitted!</h2>").append("<p>We will be in touch soon.</p>");
        }
        else {
            $('#contact_form').html("<div id='message'></div>");
            $('#message').html("<h2>Error with Submission!</h2>").append("<p>Please Try again.</p>");
        }
    }
});
return false;​

PHP

if ($count > 15)
{
    $msg = 'error';
    echo $msg;
    exit();
}
elseif ($stmt = $mysqli->prepare("INSERT INTO Comments (Name, Email, Comment) values (?, ?, ?)"))
{
    //do sql logic here 
    $msg = 'proceed';
    echo $msg;
}
else
{
    /* Error */
    printf("Prepared Statement Error: %s\n", $mysqli->error);
}
4

5 回答 5

2

您可能会在“继续”响应中输出空格或其他字符,这会使成功处理程序中的条件为假。

于 2012-10-24T12:30:41.953 回答
0
var dataString = $("form").serialize();
var msg = '<?php echo $msg; ?>';

$.ajax({
    type: "POST",
    url: 'submit_form.php',
    data: dataString,
    dataType: 'html',
    error: function() {
        alert('Error');
    },
    success: function(msg) {
        if (msg == 'proceed') {
            $('#contact_form').html("<div id='message'></div>");
            $('#message').html("<h2>Contact Form Submitted!</h2>").append("<p>We will be in touch soon.</p>");
        }
        else {
            $('#contact_form').html("<div id='message'></div>");
            $('#message').html("<h2>Error with Submission!</h2>").append("<p>Please Try again.</p>");
        }
    }
});
return false;​

尝试这个 ?

于 2012-10-24T12:35:05.123 回答
0

I had the same problem and the error is in php not ajax.

Define $MySQLi at the top of submit_form.php and try ;)

于 2014-07-01T14:17:16.803 回答
0

试试这个:使用修剪来清理响应

success: function(msg) {
    if ($.trim(msg) == 'proceed') {
        $('#contact_form').html("<div id='message'></div>");
        $('#message').html("<h2>Contact Form Submitted!</h2>").append("<p>We will be in touch soon.</p>");
    }
于 2012-10-24T12:35:20.857 回答
0

我看到你的 ajax 可能总是运行错误方法的两个最常见的原因。

 dataType=json

如果不是 json 删除此行

  async : true

考虑改变这个

于 2014-04-11T20:40:04.603 回答