Javascript 刷新页面而不重新提交表单
这可能很简单,但我对php和js真的很陌生 我为我的网站做了一个评论系统,但我有一个问题我无法解决Javascript 刷新页面而不重新提交表单,javascript,php,html,forms,page-refresh,Javascript,Php,Html,Forms,Page Refresh,这可能很简单,但我对php和js真的很陌生 我为我的网站做了一个评论系统,但我有一个问题我无法解决 //comment section $commentsArray = array(); $commentQuery_run = mysqli_query($con, "SELECT * FROM comments WHERE PostID='$userPostId'"); if (mysqli_num_rows($commentQuery_run) > 0) { echo "<b
//comment section
$commentsArray = array();
$commentQuery_run = mysqli_query($con, "SELECT * FROM comments WHERE PostID='$userPostId'");
if (mysqli_num_rows($commentQuery_run) > 0) {
echo "<b id='commcount'>Comments:".mysqli_num_rows($commentQuery_run).
"</b>";
while ($commentRow = mysqli_fetch_assoc($commentQuery_run)) {
$commentID = $commentRow['id'];
$commentUsername = $commentRow['username'];
$commentUserPfpPath = $commentRow['path'];
$commentContent = $commentRow['text'];
$commentDate = $commentRow['date'];
$commentsArray[] = $commentContent;
echo "html for displaying the comments";
}
} else {
echo "<b id='commcount'>No comments! Be the first one to comment!</b>";
}
if ($isLoggedIn === true) {
echo "<form id='commForm' method='POST' action=''> <
input id = 'commTextInp'
type = 'text'
placeholder = 'Your comment...'
name = 'commentText' > < br >
<
input id = 'commSubmInp'
type = 'submit'
name = 'commentSubmit'
value = 'Post Comment' >
<
/form>";
} else {
echo "<b id='commcount'>Please Login In to comment!</b>";
}
//comment section
//coment process
if (isset($_POST['commentSubmit'])) {
if (isset($_POST['commentText']) && !empty($_POST['commentText'])) {
$postCommentUsername = $_SESSION['username'];
$postCommentPfpImg = $_SESSION['pfpimg'];
$postCommentContents = mysqli_real_escape_string($con, htmlentities($_POST['commentText'], ENT_QUOTES));
$postCommentDate = date("d/m/Y H:i");
if (!in_array($postCommentContents, $commentsArray)) {
$postCommentQuery_run = mysqli_query($con, "INSERT INTO comments VALUES('','$userPostId','$postCommentUsername','$postCommentPfpImg','$postCommentContents','$postCommentDate')");
if ($postCommentQuery_run === true) {
echo "<script> window.location.reload() </script>";
} else {
echo "<b style='color:red;'>Error while submitting comment!</b>";
}
} else {
echo "<b style='color:red;'>Please don't repeat yourself/other users!</b>";
}
} else {
echo "<b style='color:red;'>Please write something in your comment and try again</b>";
}
}
echo "</center>";
//comment process
//注释部分
$commentsArray=array();
$commentQuery\u run=mysqli\u query($con,“从PostID='$userPostId'的注释中选择*);
如果(mysqli_num_行($commentQuery_run)>0){
echo“Comments:”.mysqli\u num\u行($commentQuery\u run)。
"";
而($commentRow=mysqli\u fetch\u assoc($commentQuery\u run)){
$commentID=$commentRow['id'];
$commentUsername=$commentRow['username'];
$commentUserPfpPath=$commentRow['path'];
$commentContent=$commentRow['text'];
$commentDate=$commentRow['date'];
$commentsArray[]=$commentContent;
echo“用于显示注释的html”;
}
}否则{
echo“无评论!第一个评论!”;
}
如果($isLoggedIn===true){
回声“<
输入id='commTextInp'
类型='text'
占位符='您的评论…'
名称='commentText'>
<
输入id='commSubmInp'
类型='submit'
名称='commentSubmit'
值='Post Comment'>
<
/表格>”;
}否则{
echo“请登录以发表评论!”;
}
//评论部分
//科门特过程
如果(isset($_POST['commentSubmit'])){
如果(设置($_POST['commentText'])和($_POST['commentText'])为空($_POST['commentText'])){
$postCommentUsername=$\会话['username'];
$postCommentPfpImg=$_会话['pfpimg'];
$postCommentContents=mysqli\u real\u escape\u字符串($\u POST['commentText'],ENT\u引号));
$postCommentDate=日期(“d/m/Y H:i”);
if(!in_数组($postCommentContents,$commentsArray)){
$postCommentQuery_run=mysqli_query($con,“插入注释值('''$userPostId','$postCommentUsername','$postCommentPfpImg','$postCommentContents','$postCommentDate'));
if($postCommentQuery\u run==true){
回显“window.location.reload()”;
}否则{
echo“提交评论时出错!”;
}
}否则{
echo“请不要重复您自己/其他用户!”;
}
}否则{
echo“请在评论中写些东西,然后再试一次”;
}
}
回声“;
//评论过程
每次我提交表单时都会出现“请不要重复您/其他用户”错误。为什么?window.location.reload()函数是否也重新提交表单?还是我做错了什么?有没有更好的方法来重新加载站点?很明显,我需要重新加载页面,以便显示新的评论。再说一次,我对php/js/html真的很陌生,所以请解释一下为什么我的代码没有按预期的方式工作。我猜reload()方法会重新提交表单(请原谅我的英语不好)您最好将后期处理代码放在文件头中,这样您就可以使用header()重定向了。要显示错误,可以使用一些标志;见:
// here we store all our comments
$commentsArray = [];
$commentQuery_run = mysqli_query($con,"SELECT * FROM comments WHERE PostID='$userPostId'");
while($commentRow = mysqli_fetch_assoc($commentQuery_run)){
$commentsArray[] = $commentRow;
}
//coment process
if(isset($_POST['commentSubmit'])){
if(isset($_POST['commentText']) && !empty($_POST['commentText'])){
$postCommentUsername = $_SESSION['username'];
$postCommentPfpImg = $_SESSION['pfpimg'];
$postCommentContents = mysqli_real_escape_string($con, htmlentities($_POST['commentText'], ENT_QUOTES));
$postCommentDate = date("d/m/Y H:i");
if(! array_search($postCommentContents, array_column($commentsArray, 'text')) ){
$postCommentQuery_run = mysqli_query($con,"INSERT INTO comments VALUES('','$userPostId','$postCommentUsername','$postCommentPfpImg','$postCommentContents','$postCommentDate')");
if($postCommentQuery_run === true){
header("Location: " . $_SERVER['PHP_SELF']);
}
else {
$is_error = 'ERROR';
}
}
else{
$is_error = 'DUPLICATE';
}
}
else{
$is_error = 'NO_DATA';
}
}
接下来,在旧位置(页面中间)可以显示错误:
if(isset($is_error)) {
switch($is_error) {
case 'DUPLICATE':
echo "<b style='color:red;'>Please don't repeat yourself/other users!</b>";
break;
case 'NO_DATA':
echo "<b style='color:red;'>Please write something in your comment and try again</b>";
break;
default:
echo "<b style='color:red;'>Error while submitting comment!</b>";
}
}
// ...........
// PRINT ALL COMMENTS HERE
if(count($commentsArray)>0){
echo "<b id='commcount'>Comments:" . count($commentsArray) . "</b>";
foreach($commentsArray as $comment){
// $comment contains all your db-fields
echo "html for displaying the comments";
}
}
else{
echo "<b id='commcount'>No comments! Be the first one to comment!</b>";
}
if(设置($is_错误)){
开关($is\u错误){
“重复”案例:
echo“请不要重复您自己/其他用户!”;
打破
案例“无数据”:
echo“请在评论中写些东西,然后再试一次”;
打破
违约:
echo“提交评论时出错!”;
}
}
// ...........
//在此处打印所有评论
如果(计数($commentsArray)>0){
回显“注释:.count($commentsArray)。”;
foreach($commentsArray作为$comment){
//$comment包含所有数据库字段
echo“用于显示注释的html”;
}
}
否则{
echo“无评论!第一个评论!”;
}
您最好将后期处理代码放在文件头中,这样您就可以使用header()重定向。要显示错误,可以使用一些标志;见:
// here we store all our comments
$commentsArray = [];
$commentQuery_run = mysqli_query($con,"SELECT * FROM comments WHERE PostID='$userPostId'");
while($commentRow = mysqli_fetch_assoc($commentQuery_run)){
$commentsArray[] = $commentRow;
}
//coment process
if(isset($_POST['commentSubmit'])){
if(isset($_POST['commentText']) && !empty($_POST['commentText'])){
$postCommentUsername = $_SESSION['username'];
$postCommentPfpImg = $_SESSION['pfpimg'];
$postCommentContents = mysqli_real_escape_string($con, htmlentities($_POST['commentText'], ENT_QUOTES));
$postCommentDate = date("d/m/Y H:i");
if(! array_search($postCommentContents, array_column($commentsArray, 'text')) ){
$postCommentQuery_run = mysqli_query($con,"INSERT INTO comments VALUES('','$userPostId','$postCommentUsername','$postCommentPfpImg','$postCommentContents','$postCommentDate')");
if($postCommentQuery_run === true){
header("Location: " . $_SERVER['PHP_SELF']);
}
else {
$is_error = 'ERROR';
}
}
else{
$is_error = 'DUPLICATE';
}
}
else{
$is_error = 'NO_DATA';
}
}
接下来,在旧位置(页面中间)可以显示错误:
if(isset($is_error)) {
switch($is_error) {
case 'DUPLICATE':
echo "<b style='color:red;'>Please don't repeat yourself/other users!</b>";
break;
case 'NO_DATA':
echo "<b style='color:red;'>Please write something in your comment and try again</b>";
break;
default:
echo "<b style='color:red;'>Error while submitting comment!</b>";
}
}
// ...........
// PRINT ALL COMMENTS HERE
if(count($commentsArray)>0){
echo "<b id='commcount'>Comments:" . count($commentsArray) . "</b>";
foreach($commentsArray as $comment){
// $comment contains all your db-fields
echo "html for displaying the comments";
}
}
else{
echo "<b id='commcount'>No comments! Be the first one to comment!</b>";
}
if(设置($is_错误)){
开关($is\u错误){
“重复”案例:
echo“请不要重复您自己/其他用户!”;
打破
案例“无数据”:
echo“请在评论中写些东西,然后再试一次”;
打破
违约:
echo“提交评论时出错!”;
}
}
// ...........
//在此处打印所有评论
如果(计数($commentsArray)>0){
回显“注释:.count($commentsArray)。”;
foreach($commentsArray作为$comment){
//$comment包含所有数据库字段
echo“用于显示注释的html”;
}
}
否则{
echo“无评论!第一个评论!”;
}
每次我提交表单时都会出现“请不要重复您/其他用户”错误。为什么?
第一条注释为true
,因为:
if(mysqli_num_rows($commentQuery_run) > 0)
对于第一条注释为false
,commentsArray
为空
每次我提交表单时都会出现“请不要重复您/其他用户”错误。为什么?
第一条注释为true
,因为:
if(mysqli_num_rows($commentQuery_run) > 0)
对于第一条注释为
false
,commentsArray
为空 window.location.reload()
-标准您是否正在处理POST请求?“您可能想这样做和/或长时间拉车。”@SamSwift웃 正如我的代码所示,我现在正在使用这个方法,并且正在给我issues@SamyokNepal没有im使用纯php/js,如代码所示。您是否尝试过完全取消重新加载?我可能错了,但我认为你在重新加载两次。提交时,是否会出现一个对话框?window.location.reload()
-您是否正在提交POST请求?“您可能想这样做和/或长时间拉车。”@SamSwift웃