2014-08-30 32 views
-2

我創建了一個表單,如果用戶沒有填寫所有的字段,目的是刷新當前頁面的頁面。問題在於第七行。php重定向表單和url的

一個頁面刷新和URL更改。該代碼將%20添加到URL中。有沒有辦法阻止這種情況?

一旦表單被sumbmited,我也想重定向用戶到一個'謝謝你的頁面',以防止他們重新提交多次使用刷新按鈕或瀏覽器後退按鈕。這是否可能和最安全的方式?該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>'; 
    } 
} 

?>

回答

0

你想實現這樣的事情?

<?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構造之後header()電話,以防止進一步的腳本執行。 另外,%20是空間的代碼。如果你想擺脫它,不要在你的網址中使用空格。

+0

感謝META,但頭功能似乎仍然沒有工作。有任何想法嗎?我已編輯原始帖子以顯示我已實施的更改 – JJJ 2014-08-30 13:33:56

+0

@ user2324730當您提交帶填充字段的表單時,您會看到什麼?重定向正在爲我工​​作。 – fakemeta 2014-08-30 13:43:13

+0

感謝,形式下頁: 的http://本地主機:8888/mycms/post_details.php後= 78 當我填寫表格,然後按透過,表格空白出來,向我發送到 http:// localhost:8888/mycms/post_details.php (在網址末尾沒有帖子ID) – JJJ 2014-08-30 13:48:31