html - PHP Form Post/Redirect/Get Header Location Error -


i have simple form i've made me learn php. i'm using local host server host php file (form.php). however, when refresh page resubmits form, know can use post/redirect/get method negate problem. except implementing header('location: form.php'); hasn't been working, if take @ code- , tell me i'm doing wrong, appreciated.

code example i.e without header('location: form.php');

<?php  if (empty($_post) === false) {     echo '<pre>', print_r($_post, true), '</pre>';     } ?>  <!doctype html> <html>   <head>     <meta charset="utf-8">     <title>form</title>   </head> <body>  <form action="form.php" method="post">   <p>     <label for="name">name:</label><br>     <input type="text" name="name" id="name"><br>   </p>   <p>     <label for="email">email:</label><br>     <input type="text" name="email" id="email"><br>   </p>   <p>     <label for="message">message:</label><br>     <textarea name="message" id="message"></textarea>   </p>   <p>     <input type="submit" value="submit">   </p> </form> 

this result...

screen shot without header location

enter image description here

then add:

header('location: form.php'); 

and this...

enter image description here

to prevent form resubmitting on page refresh, 2 methods used:

method 1: use ajax + redirect

submit form using ajax , redirect page using jquery.

method 2: reload page

refresh page using javascript.

your code should this:

    <?php          if (!empty($_post)) {             echo '<pre>', print_r($_post, true), '</pre>';             echo '<script type="text/javascript"> location.reload();</script>';         }     ?>      <!doctype html>     <html>        <head>          <meta charset="utf-8">          <title>form</title>        </head>        <body>          <form action="form.php" method="post">           <p>             <label for="name">name:</label><br>             <input type="text" name="name" id="name"><br>           </p>           <p>             <label for="email">email:</label><br>             <input type="text" name="email" id="email"><br>           </p>           <p>             <label for="message">message:</label><br>             <textarea name="message" id="message"></textarea>           </p>           <p>             <input type="submit" value="submit">           </p>         </form>        </body>     </html> 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -