I wrote a single program using html and javascript which contains a submit button. The problem that I have is that when I refresh my page, it's like I pressed submit button again!! But what I want is to NOT execut the script if I refresh the page. I don't know if it's clear but it's like that I want the program to get intialized if i refresh my page instead of executing a previous script !
I believe you are referring to the browser behaviour that is when you submit a form to the same page, if you refresh it will prompt you to resubmit the data again.
You can avoid this by issuing a redirect at the end of your script, to refresh the page and clear the history of the submit.
if (isset($_POST['submit'])) {
// ... your code here
header('Location: ' . $_SERVER['PHP_SELF']);
}
Related
I have a multi-step form which contains 4 steps. When the 3rd step is submitted an alert comes in for successful submission. So, until the alert comes up I want to show a loading page. So, how can I do so ?
Thank you
I searched a lot for loading page, but it is for until the page loads. And in this form I want to show the loading page until an alert comes up.
The solution is simple,
you need to show your loader page on successful form submission, the loading page will appear on the page and keeps displaying until the page is redirected to the next page after processing.
simple you can do it like this in js
$(document).ready(function(){
$("#myform").on("submit", function(){
$("#pageloader").fadeIn();
});//submit done
});//document ready
refer this post - Click Here
I'm a little stuck on creating an automatic page to press a 'log in' button which runs a javascript, submitting the form automatically isn't good enough as it simply doesn't log the user in (it's a javascript login system - as a test), instead, it will just return an error: http://prntscr.com/m1f1f6 it needs to automatically on load 'press' the log in button: http://prntscr.com/m1f1ur in order to perform the action of sending to "./main.php" which has an 'isLoggedIn' function.
<button class="btn btn btn-block" id="btnLoginMain">Log in</button>
Thanks in advance!
First, this is not a good approach to make the customers/users to logging in. You can use cookie or sessions to auto login your user. Still, if you want to click the button auto on web page loading you can use the below script.
<script>
window.onload=function(){
$("#btnLoginMain").click();
}
</script>
use this in the header file of your html file. If you want to click the button after some time of page load use the setTimeout() function.
You do not need click there. Check the source code, what happen when you click that button and replicate it in your script.
Clicking it is not needed to log in. Copy and paste your code for when you do press the button, and just make it appear when the page starts up.
I am aware of the many Post/Redirect/Get questions on here. This one is a bit different. I'm unable to find an answer that explains this particular JavaScript solution.
When you submit a form via POST and then hit the refresh button afterwards, the browser will prompt me to re-submit data. HOWEVER this prompt does not happen in the WordPress backend (when JS is enabled) and there is no Redirect/Get after the Post.
I've tried to show this in a series of screenshots below. It shows the first POST submit with the POST data printed on the page, and then the refresh causes a GET without any browser re-submit prompt.
When I disable JavaScript and hit refresh, I get the expected "Would you like to resubmit your data?" prompt and the refresh causes a second POST.
So wordpress is doing some JavaScript magic here to prevent POST data resubmission on refresh/back button.
Can anyone point me to the code in WordPress that shows how they achieve this using only JavaScript? I have no idea where to even start searching.
Do they do something with the pushstate?
Thanks!
Solution: WordPress uses window.history.replaceState on every page load.
This prevents the POST from running again on refresh or back button.
Nifty!
Non-WordPress proof of concept here: https://dtbaker.net/files/prevent-post-resubmit.php
Code is:
<script>
if ( window.history.replaceState ) {
window.history.replaceState( null, null, window.location.href );
}
</script>
After clicking the submit button , I have some php code that runs and uploads to a database,
is their a way to use javascript to open a modal at the end of the action.php ?
If i understand you to action async !
Browser : display modal windows
Server : action.php
And the modal is waiting the end of action.php to disappear. Looks like you need to manually submit your form with an ajax request to action.php and before sending the ajax request enable/show your modal(anyway library or way you want) and when you receive your response form the ajax request you disable the modal .
sounds good?
I was able to do it without anything fancy just good old if statements.....
i used $_SESSION['show'] as a flag , after the php does its work to upload to the db it sets the flag , when the page reloads after submit button is pressed it gets to an if statement surrounding the code that displays the modal , if flag is set , show the modal...
otherwise do normal behavior and keep it hidden....
I've just got this jquery form working, it's based inside a jquery tab, and on clicking the submit button, it submits to a mySQL database without leaving the page or anything. Now, though, I want it to redirect the user, without making them leave the tab, to a different page inside the same tab once they've submitted their details.
Does anyone know the Javascript code to do this?
Thanks!
If you're trying to load a new page into the DIV where your form was, simply call
$('#idOfDivWithForm').load('newpage.php');