How to disable a form after the first submission? - php

<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.

Related

HTML Form Submit but Prevent While Refreshing Page Resubmiting Form

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

How can a submit button made to be act as a link too?

I'm doing a Quiz project: The idea is to implement almost 25 questions in which each question occupies each HTML page with 4 radio buttons and a submit button and a reset button as well.On clicking the submit button it should take the user to the next page as well as submit the data to the server. How to achieve this dual behaviour?
I tried this:
<form action="cba.php" method="post">
<a href="abc.html">
<input type="submit" value="submit">
</a>
</form>
But this does only one purpose: Acting as a link without submitting the data.
If you just want to redirect the user after submitting the form, you can use :
header("Location: yourlink");
in the php script you called cba.php.
Otherwise, i'm not sure it is possible to redirect the user before sending him the php script page.
As mentioned, it would be a smoother experiance to handle this via ajax, but it can be acheived in just php by creating a redirect in the form processing code (as mentioned in comments and a current answer).
I believe your issue is with the fact that the same proccessing code (cba.php) will be called every step of the way, so you need a way for each quiz section to define the next section.
This can be done with a hidden field instead of the link code you tried:
<form action="cba.php" method="post">
<input type="hidden" name="next-page" value="abc.html">
<input type="submit" value="submit">
</form>
Then i cba.php, you redirect to the value contained in this hidden field:
//save the data from the form, then
header("Location: " . $_POST['next-page']);

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.

Why does my form submit to my php as a url instead of the actions in the php file

Ok I have a form with multiple submit buttons.
The coding on my php file has a header with a url depending on which form was entered. My issue is when I submit the form( no matter which button I use) the window that pops up is not the url action assigned to that button but the php file itself. What am I doing wrong?
the form starts of like this so that you can see if I directed it correctly
<form method="post" action="http://gamerzacademy.com/foodCYO.php" target="_blank">
<input type="text" name="uid">
<input type="submit" name="Dish1" value="Dish1" onclick="
this.disabled=true;
this.value='Gift Opened';
document.FreeFoodForm.submit();">
<input type="submit" name="Dish2" value="Dish2" onclick="
this.disabled=true;
this.value='Gift Opened';
document.FreeFoodForm.submit();">
etc......
now the php file starts like this
<?php
if ($_REQUEST['Dish1'] == "Dish1") {
header("Location: url1".urlencode($_POST['uid']));
}
else if ($_REQUEST['Dish2'] == "Dish2") {
header("Location: url2".urlencode($_POST['uid']));
}
else if ($_REQUEST['Dish3'] == "Dish3") {
header("Location: url3".urlencode($_POST['uid']));
}
.....etc
?>
You are posting the form through Javascript. The code doesn't know which button was clicked, so the value of that button isn't posted to the form. Therefor, your form cannot see which button was clicked. If you change the method to get, you will see which value do or do not get posted.
I think you don't need to post from Javascript at all. Just let the button do the posting. Only the name and value of the button that was clicked will be posted.
B.t.w., you disable the button, presumably because you don't want people to press the button twice, but in your setup they still can press any other button. I think it is wise to disable all of them.
Two things:
First, make sure the URLs you are sending in header are valid URLs.
Second, it looks like you have some whitespace before the <?php opening tag. Make sure there is no whitespace before the PHP opening tag. If there is, header won't work.
By doing the following in JavaScript:
this.disabled = true;
You effectively don't send its value to PHP.
A better idea might be an on submit handler in the form that prevents double submit.

Image Submit Button within a Form

I have a form that has Submit button in form of an Image. When user clicks the image button, the image button should play the role of submit button.
Code Sample:
<form action="page.php" method="POST">
<input type="image" name="btn_opentextbox" src="image.png" value="Submit" />
</form>
Handle Submission:
if($_POST['btn_opentextbox'])
{
//do something
}
Surprisingly, the above code used to work perfectly fine in Firefox. However, once i updated my Firefox yesterday, it didn't work at all. I click the button, page gets refreshed and nothing happens. The code also doesn't work in IE.
Note: it works in Chrome.
I want it to work in Firefox, IE, etc.
Any suggestions?
you can add a hidden field
<input type="hidden" name="action" value="Submit Form">
and in php you can do this
if($_POST['action'] == "Submit Form"){
do something
}
hope this help.
You should use a normal submit-button and use CSS to replace the button's look with an image. This should work in all browsers.
for image submit button
php code is
if(isset($_POST['btn_opentextbox_X']) || isset($_POST['btn_opentextbox_Y']))
{
//do something
}
The good php code is :
<input type='image' src='../images/blanc.gif' width='596' height='35' onFocus='form.submit' name='btn_opentextbox'/>
if ($_POST["btn_opentextbox_x"]) && ($_POST["btn_opentextbox_y"])
{
......
}
Check for btn_opentextbox_x or btn_opentextbox_y instead. (It is actually . not _ but PHP mangles it).
Some browsers fail to send the value for server side image maps, just the co-ordinates.
And you seem to have forgotten the alt attribute.
Alternatively, use an actual submit button instead of an image map:
<button type="submit" name="btn_opentextbox" value="submit"><img src="image.png" alt="Submit"></button>
… but note that some versions of IE will send the HTML content instead of the value when it is submitted.
Do you have multiple buttons in that form and need to know that the form was submitted?
If there is just a single submit button I suggest using following code:
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// process form submission
header('Location: page.php?result=success');
}
This way you'll be sure if form was submitted and also avoid double submission if user hits reload button after form was submitted.

Categories