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:
Use AJAX to post the data back, get the new page, and replace your body element with it, or
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 callform.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
How to Call a C# Library from Native C++ (Using C++\Cli and Ijw)
How to Configure an App to Run Correctly on a Machine With a High Dpi Setting (E.G. 150%)
Convert a String to an Enum in C#
How to Get the Application'S Path in a .Net Console Application
.Net String.Format() to Add Commas in Thousands Place For a Number
Asp.Net MVC 5 Culture in Route and Url
Processstartinfo Hanging on "Waitforexit" - Why
Using Resources Folder in Unity
What Is the Syntax For an Inner Join in Linq to Sql
Easiest Way to Read from and Write to Files
What Does Void Mean in C, C++, and C#
How to Create an Excel (.Xls and .Xlsx) File in C# Without Installing Microsoft Office
How to Convert a Unix Timestamp to Datetime and Vice Versa
How To: Execute Command Line in C#, Get Std Out Results
How Would You Count Occurrences of a String (Actually a Char) Within a String
How to Post Messages to an Sta Thread Running a Message Pump
Direct Casting VS 'As' Operator
Why Is There a Default Instance of Every Form in Vb.Net But Not in C#