繁体   English   中英

防止提交刷新并在提交后清除输入

[英]prevent refresh on submit and clear input after submit

我想建立一个网站,可以让人们在上面发表东西和评论现在我有一些问题希望我可以在不刷新页面的情况下提交评论(我找到了解决这个问题的方法但输入不会被清除)我已经搜索了很多关于在提交时防止刷新页面的信息,但没有一个适合我,我希望有人能帮助我解决另一个问题我想让网站在发布后立即显示评论(不刷新页)

直接编辑我的代码并粘贴在这里将不胜感激

下面是我的 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>&nbsp;</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 进行异步操作。 Submit 事件必须刷新页面才能重新发送新的表单数据。

PHP 是同步的,这意味着如果你想用同步代码做一些事情,你需要刷新整个页面。

进一步学习: mozila 这里的 Ajax Javascript 文档

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM