HTML Form Submit but Prevent While Refreshing Page Resubmiting Form - php

I have a simple form. I am sending the form to same page that has the form. However, after every submit process, then when I want to refresh the page manually, the browser asks me:
Do you want to re-send the form?
How can I prevent this?
foo.php
<?php echo $_POST['id']; ?>
<form action="foo.php" method="post">
<input type="text" name="id" value="">
<input type="submit" value="Send">
</form>
Thanks!

check with a condition if the form was posted, process the form and then redirect using javascript.
<?php
if (!empty($_POST["id"]( {
//do stuff
?>
<script>
location.href=("/");
</script>
<?
}
?>

Method 1: Check with a condition if the form was submited and then redirect to the same page.
if(isset($_POST['button_name_from_form'])) {
#...code
header('Location:page.php');
}
!! But make sure that header is before any output. !!
Method 2: Ajax

Related

The PHP POST variable regains its value after refreshing

So, I have a form which I submit once and redirect it withing the same page and after the refresh I execute some scripts on the data.
For ex, if my page is index.php I can summarise it like this:
<?php
if(isset($_POST['msg']))
{
//do smth
}
$_POST=array();
?>
<html>
....
<form action="index.php" method="post">
<input type="text" id="msg" name="msg">
<button type="submit">Submit</button>
</form>
....
</html>
The problem is that after the first submission, the POST variable regains its initial value after every page refreshment even though I delete it every time and I don't resubmit the form.
So what should I do?

Display confirmation message

I need to get confirmation before proceeding.
My code is:
echo "<SCRIPT> confirm('Are you sure you want to continue') </SCRIPT>";
//press ok should keep on processing the code while cancel not.
Currently any button pressed continues.
How can I validate what button in the confirmation pop up window is pressed?
You cannot.
PHP is a server-side language and JavaScript is a slient-side one.
PHP is executed on server long before JavaScript starts to execute in the clients browser.
One way to do that is to split the service in two. You don't even have to use JavaScript confirm, you can just use HTML form with submit button and a hidden value with POST method.
<form method="POST">
<input type=hidden name="internal_check" value="42">
<input type=submit value="Click me to confirm!">
</form>
After clicking the button the page will be reloaded.
In the PHP script you can check POST variables for that value and if present you can continue doing the comfirmed job using something like this:
<?php
if(isset($_POST['internal_check']) && $_POST['internal_check'] == 42)
{
/* do confimed job here */
}
else
{
?>
<form method="POST">
<input type=hidden name="internal_check" value="42">
<input type=submit value="Click me to confirm!">
</form>
<?php
}
?>
If the page was not accessed via POST call with the secret variable holding the right value the user will only see the confirmation form. Otherwise the special code will be executed.
If you are not happy with reloading the page the same may be done using AJAX. The basics are the same, but it is a bit more complicated.

How does the submit button on a form work?

I have a simple form that collects data and sends it to a PHP script using POST.
<form method="post">
<input type="text" name="cost">
<button name="submit" type="submit">Submit</button>
</form>
The PHP script is,
if(isset($_POST['submit'])){
echo "set";
}
I want to know what happens when I click on the submit button?
The PHP manual says the following about isset,
isset — Determine if a variable is set and is not NULL
When exactly is the submit button SET? When I echo out echo $_POST['submit']; it outputs nothing.
It's only when I use the value attribute along with the submit button that I get something on $_POST['submit'];. Why should I use the value with the submit button? What exactly does it do?
I want to know what happens when I click on the submit button?
It submits the form.
When exactly is the submit button SET?
When the user submit's the form.
When I echo out echo $_POST['submit']; it outputs nothing.
You didn't specified a value for it, so it returns an empty string ($_POST['submit'] === "")
Why should I use the value with the submit button? What exactly does it do?
Well on an button the value is not needed, it is enough when it is set, so you can check if the button was submitted and not an other form f.ex.
Try with this
<form action="" method="post">
<input type="text" name="cost" />
<input type="submit" name="submit" value="Submit" />
</form>
In php side
if(isset($_POST['submit']) && $_POST['submit']=="Submit"){
echo "set";
}
It submits the whole form data into targeted location and the GET and POST methods are used to send encoded data to the targeted location
The GET method is restricted to send upto 1024 characters only.
The POST method does not have any restriction on data size to be sent.

Redirect After Submit php [duplicate1]

This is my code:
<input type="submit" name="send" class="do__order" value="order" >
Now, I want to redirect the submitted to any page of my choice after the form data has been submitted, how do I do it?
Add the following line to the script:
header('Location: http://www.example.com/');
You can try something like this on page where you do your check. Let's say it's the check.php
if(isset($_POST['send'])){
header('Location: somewhere.php');
}
And you also need to add a method attribute to your form tag, in this case it would be post, so method="post" and action would be action="check.php".

How to disable a form after the first submission?

<form method="POST" action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" onsubmit="document.getElementById('submit-button').disabled=true;">
I use that line to disable the button after the first click, but it doesnt work..
Here is the line of the button:
<input type="submit" value="Register" id="submit-button"/>
I'd guess that what is happening is your code is firing but then page will refresh after the form has been submitted and the button will no longer be disabled.
If this is the case then the you could insert the disabled property in to the button's HTML from the server side when you know that the page is being rendered as the result of the form being submitted.
If you are posting to the same page and wish for the button to be disabled after the form has been submitted once, what you can do is use PHP to check if the data that was submitted by the form has been posted to the page. If it has, disable the button. It might look like this:
<form method="POST" action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" <? if (isset($_POST['your_form_data'])) echo "disabled='disabled'" ?> >
You may want to save a boolean flag in database for example isRegistered, so if the user is already registered, the form will not be shown.
Without jquery (just an example, better to use jquery):
function disablebtn( idbtn ) {
document.getElementById(idbtn).innerHTML = 'Loading...';
document.getElementById(idbtn).disabled=true;
}
<form onsubmit="disablebtn('formPageBtn')">
<button type="submit" id="formPageBtn">Send</button>
</form>
With this function when you press the button it will change the label to "loading...", too.
change the action to javascript:void%200 after the form was send!
In what way does it "not work"?
If you mean that it doesn't submit the form, the button just stays disabled, try using a setTimeout to delay the disabling slightly.
If you mean that the button is not disabling, are you sure the page isn't reloading by the form being submitted? If this is what's happening, you might want to add <?php if($_POST) echo " disabled"; ?> inside your submit button.

Categories