Page 1 of 1

Disabled submit button prevents form submission

Posted: Thu Dec 18, 2003 7:06 pm
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?

Posted: Thu Dec 18, 2003 7:24 pm
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.

Posted: Thu Dec 18, 2003 8:28 pm
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>

Posted: Thu Dec 18, 2003 8:33 pm
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.

Posted: Thu Dec 18, 2003 10:20 pm
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.)

Posted: Fri Dec 19, 2003 8:18 am
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

Posted: Fri Dec 19, 2003 1:20 pm
by qads
Robert K S wrote:Qads: No need for the loop, print_r($_POST) will do
LOL, yep your right lol.

Posted: Sat Dec 20, 2003 12:12 am
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.

Posted: Sat Dec 20, 2003 12:44 am
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