Javascript 如何使用jQuery AJAX和PHP,使用<;将表单提交到mySQL数据库;a>;标签
我想做的是为我的网站创建一个“保存”按钮,保存特定的帖子和评论,就像Reddit上的“保存”按钮一样。目前,我正在尝试自学jQueryAjax,并试图找出如何在不必重新加载整个页面的情况下将数据提交到数据库。我在这里试图做的是保存一个字符串,当我单击“保存”时,将其提交到一个名为“保存”的表中 HTMLJavascript 如何使用jQuery AJAX和PHP,使用<;将表单提交到mySQL数据库;a>;标签,javascript,php,jquery,mysql,ajax,Javascript,Php,Jquery,Mysql,Ajax,我想做的是为我的网站创建一个“保存”按钮,保存特定的帖子和评论,就像Reddit上的“保存”按钮一样。目前,我正在尝试自学jQueryAjax,并试图找出如何在不必重新加载整个页面的情况下将数据提交到数据库。我在这里试图做的是保存一个字符串,当我单击“保存”时,将其提交到一个名为“保存”的表中 HTML <div id="message1"> <div id="pmessage"><p><?php echo $myComment;?>
<div id="message1">
<div id="pmessage"><p><?php echo $myComment;?></p></div>
<a href="#" class="Save" style="color: blue;">Save</a>
<a href="#" class="Edit" style="color: blue;">Edit</a>
<a href="#" class="Hide" style="color: blue;">Hide</a>
</div>
<form action="ajaxexample.php" method="post" style="display: none" id="1234">
<input type="hidden" name="message" id="message" value="<?php echo $myComment; ?>">
</form>
PHP5.3
$message = $_POST['message'];
$query = "INSERT INTO saved (comment) VALUES (?)";
$statement = $databaseConnection->prepare($query);
$statement->bind_param('s', $message);
$statement->execute();
$statement->store_result();
$submissionWasSuccessful = $statement->affected_rows == 1 ? true : false;
if ($submissionWasSuccessful)
{
header ("Location: index.php");
}
$myComment = "This is my message!";
到目前为止,我所要做的只是将消息“This is my message!”提交到数据库表“Saved”中。我的代码有什么问题?为什么我不能将数据提交到表中,如何修复它?提前谢谢 Submit
form
当有人点击a.Save
$('a.Save').click(function (e) {
e.preventDefault();
$("#1234").submit();
});
提交表格上的处理程序#1234
$("#1234").submit(function(e) {
e.preventDefault();
$.ajax({
type: "POST",
url: 'ajaxexample.php',
data: $("#1234").serialize(),
success: function(data)
{
// data stores the response from ajaxexample.php
// Change the html of save by using $("a.Save").html("Unsave");
}
});
});
序列化
自动生成查询字符串
$(".save").bind("click",function(e){e.preventDefault();
$.ajax({
url : $("#1234").attr("action"),
type : "POST",
data : $("#1234").serialize(),
success : function(data){},
fail : function(data){},
});
});
$(".save").bind("click",function(e){e.preventDefault();
$.ajax({
url : $("#1234").attr("action"),
type : "POST",
data : $("#1234").serialize(),
success : function(data){},
fail : function(data){},
});
});