使用AJAX请求发送的表单数据不更新sql表

时间:2017-03-30 14:21:30

标签: php jquery html sql ajax

我在使用从Jquery AJAX POST发送的表单数据更新sql表时遇到问题。

我尝试过mysql_error()并且没有返回任何错误。

我在我的textarea“-Test Test”

中增加了一行

这是在表单数据上识别的 enter image description here

在底部你会看到测试文本,这是输入到notetext1中的文本的一部分,然后在 submitNoteText.php 中将其作为notetext2:

<?php include 'connectionDetails.php'; ?>

<?php


if (isset($_POST['noteid1'], $_POST['notetext1'])) 
{

    $noteid2 = $_POST['noteid1'];
    $notetext2 = $_POST['notetext1'];

    $query = "UPDATE Notes SET Note = " . $notetext2 . " WHERE NoteID = ".$noteid2;
    $stmt = sqlsrv_query($conn, $query) or die(mysql_error());
}

sqlsrv_close($conn);

?>

然而,我在我的数据库中刷新了我的表,尽管成功发布了帖子,但还没有添加文本? enter image description here

JQuery:

function submitNoteText()
{
    var noteid = <?php if(isset($_POST['noteid'])){ echo $_POST['noteid'];} ?>;
    var notetext = $("#ta1").val();

    var dataString = 'noteid1=' + noteid + '&notetext1=' + notetext;

    console.log("NoteID: " + noteid);

    if(noteid == ''||notetext == '')
    {
        alert("NoteID or Text is blank");
    }
    else
    {   
        $.ajax({
            type: "POST",
            url: "submitNoteText.php",
            dataType: 'json',
            data: dataString,
            cache: false,
            success: function(result){
                alert("Successfully saved!");
            }
        });
    }
    return false;
};

1 个答案:

答案 0 :(得分:0)

尝试将数据作为对象而不是字符串发送。 所以改变这一行:

var dataString = 'noteid1=' + noteid + '&notetext1=' + notetext;

var data = {noteid1: noteid, notetext1: notetext};

然后您应该更改使用此数据的请求,使其变为:

    $.ajax({
        type: "POST",
        url: "submitNoteText.php",
        dataType: 'json',
        data: data,
        cache: false,
        success: function(result){
            alert("Successfully saved!");
        }
    });