php重定向表单和url';s


php redirect form and url's

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

一个是刷新页面并更改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>

您应该始终在header()调用之后调用exit构造,以防止进一步执行脚本。此外,%20是用于空间的代码。如果你想去掉url,就不要在url中使用空格。