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

「続行」応答とともに空白またはその他の文字を出力している可能性があります。これにより、成功ハンドラーの条件が false になります。

于 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

これを試してください:トリムを使用して応答をきれいにします

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 に答える