[英]Form data sent with AJAX request not updating sql table
我有一个问题是使用从Jquery AJAX POST发送的表单数据来更新sql表。
我试过mysql_error()并且它没有返回任何错误。
我在textarea“-Test Test”中增加了一行
在底部,您会看到测试文本,这是输入到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);
?>
但是,我在我的数据库中刷新我的表格,尽管成功发布了帖子但仍没有添加文本?
JQuery:
function submitNoteText()
{
var noteid = <?php if(isset($_POST['noteid'])){ echo $_POST['noteid'];} ?>;
var notetext = $("#ta1").val();
var dataString = 'noteid1=' + noteid + '¬etext1=' + 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;
};
尝试将数据作为对象而不是字符串发送。 所以改变这一行:
var dataString = 'noteid1=' + noteid + '¬etext1=' + 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!");
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.