25

I have a little problem. I want to reload my page after submitting a form.

<form method="post" action="">
   <textarea cols="30" rows="4" name="update" id="update" maxlength="200" ></textarea>
   <br />
   <input type="submit"  value=" Update "  id="update_button"  class="update_button"/>
</form>
Cœur
  • 37,241
  • 25
  • 195
  • 267
Dário Viegas
  • 289
  • 1
  • 3
  • 3

9 Answers9

73

only use

 echo "<meta http-equiv='refresh' content='0'>";

right after insert query before } example

if(isset($_POST['submit']))
        {
SQL QUERY----
echo "<meta http-equiv='refresh' content='0'>";
}
Gul Nawaz
  • 739
  • 5
  • 2
  • Nice this really helped me out! – Tomm Nov 13 '17 at 14:05
  • this is a true "refresh" and therefore answers the question, whereas many of the other answers require a new URL to be created as $location. For someone with dozens of variables in the URL, this could be time consuming and tiresome, etc. – user2954658 Oct 18 '19 at 16:46
  • 1
    hey this worked for me, very good. However, it now refreshes twice, once from the form, which refresh doesn't really work. the other from the echo code. Did u manage to stop the first one from refreshing? – Zack Cheang Weng Seong Oct 17 '20 at 17:30
11
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <!-- notice the updated action -->
<textarea cols="30" rows="4" name="update" id="update" maxlength="200" ></textarea>
<br />
<input name="submit_button" type="submit"  value=" Update "  id="update_button"  class="update_button"/> <!-- notice added name="" -->
</form>

on your full page, you could have this

<?php

// check if the form was submitted
if ($_POST['submit_button']) {
    // this means the submit button was clicked, and the form has refreshed the page
    // to access the content in text area, you would do this
    $a = $_POST['update'];

    // now $a contains the data from the textarea, so you can do whatever with it
    // this will echo the data on the page
    echo $a;
}
else {
    // form not submitted, so show the form

?>

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <!-- notice the updated action -->
<textarea cols="30" rows="4" name="update" id="update" maxlength="200" ></textarea>
<br />
<input name="submit_button" type="submit"  value=" Update "  id="update_button"  class="update_button"/> <!-- notice added name="" -->
</form>

<?php

} // end "else" loop

?>
Tom Groot
  • 1,160
  • 1
  • 9
  • 26
cantsay
  • 1,967
  • 3
  • 21
  • 34
7

If you want the form to be submitted on the same page then remove the action from the form attributes.

<form method="POST" name="myform">
 <!-- Your HTML code Here -->
</form>

However, If you want to reload the page or redirect the page after submitting the form from another file then you call this function in php and it will redirect the page in 0 seconds. Also, You can use the header if you want to, just make sure you don't have any content before using the header

 function page_redirect($location)
 {
   echo '<META HTTP-EQUIV="Refresh" Content="0; URL='.$location.'">';
   exit; 
 }

 // I want the page to go to google.
 // page_redirect("http://www.google.com")
Othman
  • 2,942
  • 3
  • 22
  • 31
  • use the first one in `HTML` in case you want the page refresh after clicking submit. use the second one if you want after clicking submit you want to change the page. it's your choice. – Othman May 18 '12 at 15:33
6

LOL, I'm just wondering why no one had idea about the PHP header function:

header("Refresh: 0"); // here 0 is in seconds

I use this, so user is not prompt to resubmit data if he refresh the page.

See Refresh a page using PHP for more details

George G
  • 7,443
  • 12
  • 45
  • 59
4

You can maybe use :

<form method="post" action=" " onSubmit="window.location.reload()">
SoEnLion
  • 185
  • 5
3
   <form method="post" action="">
   <table>
   <tr><td><input name="Submit" type="submit" value="refresh"></td></tr>
   </table>
   </form>

<?php
    if(isset($_POST['Submit']))
    {
    header("Location: http://yourpagehere.com");
    }
?>
Neku
  • 75
  • 9
1

action attribute in <form method="post" action="action="""> should be just action=""

Kristian
  • 21,204
  • 19
  • 101
  • 176
0

You want a form that self submits? Then you just leave the "action" parameter blank.

like:

<form method="post" action="" />

If you want to process the form with this page, then make sure that you have some mechanism in the form or session data to test whether it was properly submitted and to ensure you're not trying to process the empty form.

You might want another mechanism to decide if the form was filled out and submitted but is invalid. I usually use a hidden input field that matches a session variable to decide whether the user has clicked submit or just loaded the page for the first time. By giving a unique value each time and setting the session data to the same value, you can also avoid duplicate submissions if the user clicks submit twice.

Kasapo
  • 5,294
  • 1
  • 19
  • 21
0
  //insert this php code, at the end after your closing html tag. 

  <?php
  //setting connection to database
  $con = mysqli_connect("localhost","your-username","your-
  passowrd","your-dbname");

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

     $txt_area = $_POST['update']; 

       $Our_query= "INSERT INTO your-table-name (field1name, field2name) 
                  VALUES ('abc','def')"; // values should match data 
                 //  type to field names

        $insert_query = mysqli_query($con, $Our_query);

        if($insert_query){
            echo "<script>window.open('form.php','_self') </script>"; 
             // supposing form.php is where you have created this form

          }



   } //if statement close         
  ?>

Hope this helps.

Pang
  • 9,564
  • 146
  • 81
  • 122
Ranjan
  • 11
  • 2