javascript - 防止刷新提交并在提交后清除输入
问题描述
我想建一个可以让人们在上面发表东西和评论的网站我有一些问题我希望我可以在不刷新页面的情况下提交评论(我找到了解决这个问题的方法但是输入不会被清除)我已经搜索了很多关于防止提交时刷新页面的信息,但没有人适合我,我希望有人可以帮助我解决另一个问题我想让网站在发布后立即显示评论(不刷新页)
直接编辑我的代码并粘贴到这里将不胜感激
下面是我的 index.php
<?php
session_start();
require_once 'db.php';
require_once 'new_tdb.php';
//$datas = get_publish_article();
if(isset($_COOKIE["mcsh"])){
}else{
header("Location: ../mcsh/login/index.php");
exit();
}
?>
<html>
<head>
<title>
website
</title>
<link rel="shortcut icon" href="icon.png" type="image/x-icon" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
<?php include 'menu.css' ?>
</style>
</head>
<body>
<?php include 'menu.php' ?>
<center>
<font class="corn-blue">
</font>
</center>
<p> </p>
<?php
$bij = "SELECT * FROM `post` WHERE `comment` != 1 order by id DESC";
$result = mysqli_query($conn, $bij);
$rows = mysqli_num_rows($result);
while($row = mysqli_fetch_array($result)){
$post_id = $row['id'];
$ins = $row['ins'];
$s_uid = $row['s_uid'];
$p_id = $row['id'];
//$selc = "SELECT * FROM `post` WHERE `post_id` = $p_id";
?>
<center>
<table>
<tr>
<th>
Poster: <?php echo $s_uid;?>
</th>
</tr>
<tr>
<td>
<?php echo $ins;?>
</td>
</tr>
<tr>
<td>
<details>
<summary>Comment</summary>
<form id="worker" action="comment.php" method="post" target="the_iframe">
<input type="hidden" name="post_id" value="<?php echo $p_id; ?>">
<p><input type="text" name="comment" placeholder="Comment......"> <input type="submit" hidefocus="true" name="submit"></p>
</form>
<iframe id="is_iframe" name="the_iframe" style="display: none;"></iframe>
<?php
$sel = "SELECT * FROM `post` WHERE `post_id` = $post_id order by id DESC";
$result2 = mysqli_query($conn, $sel);
$rows2 = mysqli_num_rows($result2);
while($row2 = mysqli_fetch_array($result2)){
$ins2 = $row2['ins'];
$poster2 = $row2['s_uid'];
?>
<p><?php echo $poster2 . ': ' . $ins2; ?></p>
<?php } ?>
</details>
</td>
</tr>
</table>
</center>
<h1>
-
</h1>
<?php
}
?>
<?php if (empty($row['file'])){
}else{
echo('<img src="image/'); echo $row['file']; echo('"width="100%" height="auto">');
}
?>
</body>
</html>
解决方案
正如@Barmar 所说,您想在您的网站中使用 AJAX 进行异步操作。提交事件必须刷新页面才能重新发送新的表单数据。
PHP是同步的,这意味着如果你想用同步代码做一些事情,你需要刷新整个页面。
进一步了解: mozila 的 Ajax Javascript 文档
推荐阅读
- flutter - 如何使半透明的应用栏颤动
- angular - 延迟加载模块Angular 7中的嵌套路由破坏了后退导航
- python - 赋值前引用的局部变量“y”
- android - 应用未使用敏感权限但无法释放
- python - 我刮掉了标题和价格以及链接和信息表,当我编写 csv 文件时,我得到重复的标题、价格和链接
- visual-studio-code - 如何使用 vscode 调试 Microsoft Word Web 插件?
- c# - 将 Azure Blob 直接上传到 SFTP 服务器,无需中间文件
- vue.js - 在 Nuxtjs 中使用组件
- python - 使用 Boto3 回调,显示上传进度
- c# - 如何在 C# 中转义反斜杠双引号