Does Page Reload Ever Cause Post

Does page reload ever cause post?

Yes. If the page was loaded using POST data this will occur. To prevent this you need to implement the POST/REDIRECT/GET pattern.

Post/Redirect/Get (PRG) is a web development design pattern that prevents some duplicate form submissions, creating a more intuitive interface for user agents (users). PRG supports bookmarks and the refresh button in a predictable way that does not create duplicate form submissions.

Refreshing a page in a browser yields POST or GET request?

This is dependant on the last call that was made by the browser to get the current data. Eg:

a) If you submitted a form, performing a POST and then hit refresh, the browser will do another POST.

b) If you just clicked a link that took you to another page, performing a GET, you'll a refresh will perform a get.

If you're just starting out understanding the GET/POST methods, there is a nice pattern that you should understand that will help you not get in situations where data is posted again and again by users who constantly refresh the browser after a POST:

http://en.wikipedia.org/wiki/Post/Redirect/Get

and

An example specifically for asp.net MVC

Form Posts Every Time Page Reloads

This is because you're not following the POST/REDIRECT/GET pattern. If a POST request is successful, you should ALWAYS redirect.

Example:

$success = $obj->doSomething($_POST['var']);

if($success){
header('Location: page.php?m=success');
exit;
} else{
//Something went wrong...
}

JavaScript location.reload() is losing post data

window.location.reload() issues a GET, so yes, the POST data will be lost.

The only ways you can issue a post are either:

  1. Use AJAX to post the data back, get the new page, and replace your body element with it, or

  2. Create a form element on the page with the action being the current window.location.href, and place the data you want to post back inside it as hidden inputs. Then, on currency change call form.submit()

What happens when we refresh a web page?

Once a request is made to the server from the browser the page is processed. Even if the user cancels or stops the request, the server continues to process the request. If the user reloads/refreshes the page, it's another request that will be executed in parallel with the first request.

Even in the case of PHP, the server isn't actively checking if the user has aborted the connection. The server only knows it's been aborted when it attempts to return the results of the request.

The internet is a disconnected environment. The server doesn't know anything about the browser. The only thing the server knows is a request has been made and it must fill the request.

Why is my page refreshing after submitting a form?

Thank you @saman and @gpopoteur,

Your answers are great and do work, just wasn't working on my testing server.

This is what got to work in the end.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"    type="text/javascript"></script><script>

$('#form1').submit(function(e)
{
event.preventDefault();
$.ajax({
type : 'POST',
url : 'process.php',
data : $(this).serialize(),
success : function(response) {

}
});
});

What i did find is though that nothing is returned from the script. E.I any validation info or returned data? Not too much of a problem and i'll create a work around. Thank again

Why is my HTML page reloading every time a new entry is submitted into a form?

<input type='submit'> causes the page refresh. Replace it with <input type='button'>.

Read more here.

        // Gloabal Variables    var enteredName,     countOutput,     count,        table,     form,     allNames = [];        function project62Part2() {        // Your code goes in here.        function getElements() {      form = document.getElementById("nameForm");      countOutput = document.getElementById("numNames");      table = document.getElementById("table");     }             function addName() {      enteredName = form.name.value;      allNames.push(enteredName);     }          function countNames() {      // Reset count      count = 0;            // Loop through and count names      for (i = 0; i < allNames.length; i++) {       count++;      }     }          function output() {      // Reset table      table.innerHTML = "<tr><th>Names</th></tr>";      // Display count      countOutput.innerHTML = "Total names entered: " + count;            // Loop through and add to table display      for (i = 0; i < allNames.length; i++) {       table.innerHTML += "<tr><td>" + allNames[i] + "</td></tr>";      }     }          // Call code     getElements();     addName();     countNames();     output();         // Prevent page from reloading     return false;    }
    <form id="nameForm" action="6.2projectpart2.html#">        <label class="formLabel" for="name">Name: </label>        <input id="name" name="name" />                <input type="button" name="runExample" value="Submit" class="formatSubmit" onclick="project62Part2()" />    </form>        <div id="numNames">Total names entered: </div>        <table id="table"></table>


Related Topics



Leave a reply



Submit