gpt4 book ai didi

javascript - Ajax Jquery 评论页面

转载 作者:行者123 更新时间:2023-11-28 01:43:58 25 4
gpt4 key购买 nike

我在这里看过很多关于 SO 的帖子,我认为我所拥有的可以在使用 AJAX 发送表单数据而无需刷新页面方面发挥作用。不幸的是它不起作用,我不知道出了什么问题,所以这是我的代码:

配置文件.php

<script>
$(function () {
$('form#commentform').on('commentsubmit', function(e) {
$.ajax({
type: 'post',
url: 'insertcomment.php',
data: $(this).serialize(),
success: function () {
alert('MUST ALERT TO DETERMINE SUCCESS PAGE');
$("#comment").val('');
}
});
e.preventDefault();
});
});
</script>


<form id='commentform' method='post'>
<textarea class='comment' id='comment'></textarea>
<input type='hidden' name='activityid' value='$activityid'>
//$activityid is the ID of the status so the database knows what status ID to connect the comment with
<input type='submit' name='commentsubmit' value='Comment'>
</form>

插入评论.php

<?php
include 'header.php';


$activityid=htmlspecialchars($_POST['activityid'], ENT_QUOTES);
$comment=htmlspecialchars($_POST['comment'], ENT_QUOTES);


$commentsql=$conn->prepare('INSERT INTO wp_comments (user_id, activity_id, comment, datetime) VALUES (:userid, :friendid, :comment, CURRENT_TIMESTAMP)');
$commentsql->bindParam(':userid', $_SESSION['uid']);
$commentsql->bindParam(':activityid', $activityid);
$commentsql->bindParam(':comment', $comment);
$commentsql->execute();


include 'bottom.php';
?>

最终结果希望是评论被插入数据库而不刷新页面,然后重置文本区域。

到目前为止,当我单击评论提交按钮时,它会刷新页面。

最佳答案

试试这个:

   $(document).ready(function(){
$('form#commentform').submit(function( e ) {
var postData = $(this).serializeArray();
$.ajax({
type: 'post',
url: 'insertcomment.php',
data: postData,
success: function () {
alert('MUST ALERT TO DETERMINE SUCCESS PAGE');
$("#comment").val('');
}
});
e.preventDefault();
});
});

关于javascript - Ajax Jquery 评论页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20547155/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com