简体   繁体   中英

Form data sent with AJAX request not updating sql table

Im having an issue getting a sql table to update with the form data sent from a Jquery AJAX POST.

I've tried mysql_error() and it returns no errors.

I added an additional line to my textarea "-Test Test"

this is being recognised on the Form data 在此输入图像描述

At the bottom you see test text, this is part of the text being entered into notetext1 which is then made notetext2 in this submitNoteText.php :

<?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);

?>

However i refresh my table in my database and the text has not been added despite the successful post? 在此输入图像描述

The 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;
};

Try sending the data as an object instead of a string. So change this line:

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

to

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

And then you should change the request to use this data so it becomes:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM