php重定向表单和url的

php重定向表单和url的,php,forms,Php,Forms,我创建了一个表单,如果用户没有填写所有的字段,目的是刷新当前页面的页面。问题出在第七行 一是页面刷新,url更改。代码将%20添加到URL中。有什么办法可以防止这种情况发生吗 一旦表单提交完毕,我还想将用户重定向到“谢谢页面”,以防止他们使用“刷新”按钮或“浏览器后退”按钮多次重新提交表单。这是可能的也是最安全的方式吗?header函数似乎不起作用 非常感谢,我非常感谢大家的努力 <h2>post a comment</h2> <form met

我创建了一个表单,如果用户没有填写所有的字段,目的是刷新当前页面的页面。问题出在第七行

一是页面刷新,url更改。代码将%20添加到URL中。有什么办法可以防止这种情况发生吗

一旦表单提交完毕,我还想将用户重定向到“谢谢页面”,以防止他们使用“刷新”按钮或“浏览器后退”按钮多次重新提交表单。这是可能的也是最安全的方式吗?header函数似乎不起作用

非常感谢,我非常感谢大家的努力

    <h2>post a comment</h2>

     <form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">


    <table width "600" bgcolor="99CCCC">


        <tr>
            <td>Your Name:</td>
            <td><input type="text" name="comment_name"/></td>
        </tr>

         <tr>
            <td>Your email:</td>
            <td><input type="text" name="comment_email"/></td>
        </tr>

         <tr>
            <td>Your Comment:</td>
            <td><textarea name="comment" cols="35" rows="16"/></textarea></td>
        </tr>

          <tr>
            <td><input type="submit" name="submit" value="postcom"/></td>
        </tr>
    </table>
</form> 


 <?php                               


    $errors = [];

    if(isset($_POST['submit'] )) {

     // php scheck if comment is set 

    $comment_name = $_POST['comment_name'];
    $comment_email = $_POST['comment_email'];
    $comment = $_POST['comment'];
    $status ="unapprove";  // This is a variable called status

   if($comment_name=='' OR $comment_email=='' OR $comment=='') {   

   $errors[] = 'field is empty!';

  // echo "<script>window.open('post_details.php?post=$post_id')</script>";

    } else {


   header('Location: www.google.com');
    exit; // prevents rest of code execute

    }

}

 if(count($errors) > 0) {
    foreach ($errors as $error) {
         echo $error . '<br>';
    }
 }

?>

您是否正在尝试实现这样的目标

<?php 

$errors = [];

if(isset($_POST['submit'])) {

    $foobar = $_POST['foobar'];

    if(empty($foobar)) {
        $errors[] = 'Foobar field is empty!';
    } else {
        // Do some stuff
        header('Location: thankyoupage.php');
        exit;
    }

}

if(count($errors) > 0) {
    foreach($errors as $error) {
        echo $error . '<br>';
    }
}

?>

<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <input type="text" name="foobar">
    <input type="submit" name="submit">
</form>
您应该始终在头调用之后调用exit构造,以防止进一步执行脚本。
另外,%20是一个空格代码。如果您想删除url,请不要在url中使用空格。

谢谢,但标题功能似乎仍然不起作用。有什么想法吗?我编辑了原始帖子,以显示我所做的更改implemented@user2324730提交带有填充字段的表单时,您会看到什么?重定向正在为我工作。谢谢,表单在下一页:当我填写表单并按sumbit时,表单将清空,并在结束时将我发送到no post idurl@user2324730您的脚本是否知道要重定向到的post id?尝试标题“Location:mypage.php?post=”$post_id;。