我是ajax的新手,在提交表单时遇到问题,通过ajax发送数据。 从数据库获取数据不是问题。但插入不会工作,我没有发现错误。
我的表单文件
<script type="text/javascript">
$(document).ready(function(){
function showComment(){
$.ajax({
type:"post",
url:"./includes/ajax.comments.php",
data:"post_id=<?PHP echo $post->post_id ?>&action=show",
success:function(data){
$("#comments").html(data);
}
});
}
showComment();
$("#fastReply").click(function(e){
e.preventDefault();
var comment_text = $("#txtArea").val();
var comment_user_id = <?PHP echo $current_user->user_id; ?>;
var comment_post_id = <?PHP echo $post->post_id; ?>;
$.ajax({
type:"post",
url:"./includes/ajax.comments.php",
data: "comment_title=null&comment_text="+comment_text+"&comment_user_id="+comment_user_id+"&comment_post_id="+comment_post_id+"comment_status=1&action=add",
success: function(data){
showComment();
}
});
});
});
</script>
<form action="post">
<textarea id="txtArea"></textarea>
<button id="fastReply" type="submit">Post</button>
</form>
我的php文件看起来像这样
if($action == "add"){
header("Content-Type: application/json", true);
$comment_title = $_POST["comment_title"];
$comment_text = $_POST["comment_text"];
$comment_user_id = $_POST["comment_user_id"];
$comment_post_id = $_POST["comment_post_id"];
$comment_status = $_POST["comment_status"];
$query = $mysqli->query("INSERT INTO comments(comment_title, comment_text, comment_user_id, comment_post_id, comment_status)
VALUES('".$comment_title."', '".$comment_text."', ".$comment_user_id.", ".$comment_post_id.", ".$comment_status.")");
if($query){
echo "Your comment has been sent";
} else {
echo "Error in sending your comment";
}
}
我也找不到其他有助于我的问题
答案 0 :(得分:1)
尝试以下
function showComment(id){
$.ajax({
type:"post",
url:"./includes/ajax.comments.php",
data:{post_id:id,action:'show'},//convert to object
success:function(data){
$("#comments").html(data);
}
});
}
$("#fastReply").click(function(e){
e.preventDefault();
var comment_text = $("#txtArea").val();
var comment_user_id = '<?PHP echo $current_user->user_id; ?>';//convert to string
var comment_post_id = '<?PHP echo $post->post_id; ?>';//convert to string
$.ajax({
type:"post",
url:"./includes/ajax.comments",
data: {
comment_title: null,
comment_text: comment_text,
comment_user_id: comment_user_id,
comment_post_id: comment_post_id,
comment_status: 1,
action: 'add'
},//convert to object
success: function(data){
showComment(id);//pass the id
}
});
});
腓
$action = $_POST['action'];//get the action value
答案 1 :(得分:0)
你错过了&#34; includes / ajax.comments&#34;上的文件扩展名。在fastReply点击时触发的第二个AJAX调用,因此不会运行
$("#fastReply").click(function(e){...//this section
//other code
$.ajax({
type:"post",
url:"./includes/ajax.comments.php",//this is where it is missing & i added it
data: "comment_title=null&comment_text="+comment_text+"&...
答案 2 :(得分:0)
试试这个:
if($_POST["action"] == "add")
{
header("Content-``Type: application/json", true);
$comment_title = $_POST["comment_title"];
$comment_text = $_POST["comment_text"];
$comment_user_id = $_POST["comment_user_id"];
$comment_post_id = $_POST["comment_post_id"];
....
}