Disabled submit button prevents form submission

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
Robert K S
Forum Newbie
Posts: 11
Joined: Thu Dec 18, 2003 7:06 pm
Location: Cleveland, Ohio

Disabled submit button prevents form submission

Post by Robert K S »

I have a web form that submits to itself--a guestbook, for example. I have tried all the usual JavaScript techniques for disabling the Submit button (to prevent inadvertant multiple submissions), but they also prevent proper functioning of the page.

The PHP code in the page that tests whether the form has been submitted is

if (isset($_POST['submit']) && $_POST['submit'] == 'Add guestbook entry') {
// Error-check, then insert entry into database rather than displaying the form again
}

But this condition always fails, and I'm wondering why. Is it because the submit button has been disabled?

Can anyone help?
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post by qads »

you could test it out with this:

Code: Select all

<?php
foreach($_POST as$key => $value)
{
echo $key ." => ". $value."<br />";
}
?>
if you dont see the submit button after submitting then its prolly cos of disableing it.
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

There is a real easy way of doing this which I tend to use now and again to prevent the same form being submitted more than once. I normally use it on forms that upload graphics to a site (like avatars for example).

What you do is add a hidden input to the form and check it's value when the form is submitted. Here's an example...

** Notice I don't use a true "submit" button.

** The <fieldset> is optional obviously but is required for XHTML documents.

Code: Select all

<form id="myForm" method="POST" action="somewhere.php">
<fieldset>

<input type="hidden" name="sent" value="no" />
<input type="text" name="anInput" value="aValue" />

<input type="button" value="SUBMIT" onclick="if(this.form.elements.sent.value=='no') &#123;this.form.elements.sent.value='yes'; this.form.submit()&#125;" />

</fieldset>
</form>
Robert K S
Forum Newbie
Posts: 11
Joined: Thu Dec 18, 2003 7:06 pm
Location: Cleveland, Ohio

Post by Robert K S »

Qads: No need for the loop, print_r($_POST) will do. In any case, what would the workaround be? Has anyone else use gotten a script that submits to itself to work with a disabling submit button?

Gen-ik: There's no XHTML requirement for the <fieldset> tag--it's just a neat way to group form data when combined with the <label> tag.
Last edited by Robert K S on Sat Dec 20, 2003 12:09 am, edited 1 time in total.
Robert K S
Forum Newbie
Posts: 11
Joined: Thu Dec 18, 2003 7:06 pm
Location: Cleveland, Ohio

Post by Robert K S »

if ($_SERVER['REQUEST_METHOD'] == 'POST')

is the conditional that should be used.

Problem solved, and thanks to John Holmes. (Hope this helps somebody else in the future.)
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

Robert K S wrote:Gen-ik: There's no XHTML requirement for the <fieldset> tag--it's just a neat way to group form datat when combined with the <label> tag.
There is mate. It might just be for Strict XHTML but it's definatly required.
I've checked and tested it at W3Schools.com
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post by qads »

Robert K S wrote:Qads: No need for the loop, print_r($_POST) will do
LOL, yep your right lol.
Robert K S
Forum Newbie
Posts: 11
Joined: Thu Dec 18, 2003 7:06 pm
Location: Cleveland, Ohio

Post by Robert K S »

"I've checked and tested it at W3Schools.com"

So what you're saying is, you added an extraneous <fieldset> tag to a form and the validator didn't return an error. Why should that surprise you?

I bet if you remove the <fieldset> tag the form will validate just the same. <fieldset> is merely a graphical shortcut for drawing a pretty box around whatever elements it encloses. Actually, I don't find it very aesthetically pleasing at all--so it's good it's not an XHTML requirement.
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

Robert K S wrote:"I've checked and tested it at W3Schools.com"

So what you're saying is, you added an extraneous <fieldset> tag to a form and the validator didn't return an error. Why should that surprise you?

I bet if you remove the <fieldset> tag the form will validate just the same. <fieldset> is merely a graphical shortcut for drawing a pretty box around whatever elements it encloses. Actually, I don't find it very aesthetically pleasing at all--so it's good it's not an XHTML requirement.
No it was the other way around.

The first time I tested a Strict XHTML page with a <form> in it (with the W3C validator) it spat out an error telling me that the <fieldset> was missing from the <form>.

I agree that they don't look nice so I just 'hide' them with a <style>. If you set the border and margin to 0px then the <fieldset> doesn't show up.

Give it a try. Create a simple Strict XHTML page with a form in it (and no fieldset) and see what the validator tells you. You'll find it here... http://www.w3schools.com/site/site_validate.asp
Post Reply