起初我想为弱小的英语道歉。我想使用jQuery ajax,但我不知道如何使用它。我想在没有预加载的情况下将数据表单发送到addcomment.php。
<?php
require_once('config/config.php');
if($_POST['sent']){
$nick = $_POST['nick_ajax'];
$content = $_POST['content_ajax'];
$postID = $_POST['id_content_ajax'];
$sql_comment = $PDO->prepare('insert into `comments` (`nick`,`content`,`postID`) values (:nick,:content,:id)');
$sql_comment->execute(array(':nick' => $nick, ':content' => $content, ':id' => $postID));
header('Location: home.php?success');
}
else {
header('Location: home.php?fail');
die();
}
?>
以下代码位于home.php
<form action="addcomment.php" method="POST">
<input type="hidden" name="nick" id="nick_ajax" value="'.$_SESSION['user'].'" />
<input type="text" style="max-width: 1100px;" id="content_ajax" class="form-control" placeholder="Write a comment" name="content" /><br />
<input type="hidden" name="id" id="id_content_ajax" value="'.$article['id'].'" />
<input type="submit" id="smb_ajax" class="btn btn-default" style="float:right; margin-top: -15px;"name="sent" value=SEND" />
</form>
现在我有了这段代码
$(document).ready(function(){
$("#smb_ajax").click(function(){
var user = document.getElementById("nick_ajax").Value;
var content = document.getElementById("content_ajax").Value;
var id_cnt = document.getElementById("id_content_ajax").Value;
var todo = 0;
$.ajax({
method: "POST",
type: 'POST',
url: "addcomment.php",
data: {
'user': user,
'content': content,
'id_cnt':id_cnt
},
error: function() {
alert('Error!');
}
})
});
});