View Full Version : Check If Form Doesnt Submit

08-21-2011, 05:57 PM
To check if a form submitted you could do something like.

If (isset($_POST['submit']))

But how can you check if the form DOESN'T submit?

Say I wanted to do something similar to.

if form doesn't submit echo failure message
elseif run this command


08-21-2011, 06:08 PM
You would use an else statement or negate the isset with a !.
Older versions of IE are flawed in that they do not guarantee providing the successful submit entry. So if that is an actual submit button and its submitted by pressing the enter key in a non-multiline input, it will neglect sending the submit entry with the successful fields.

08-21-2011, 06:26 PM
So this will allow hitting the enter key

if (!($_POST['submit']))


Is it considered sloppy coding if I put an if statement inside an if statement?

08-21-2011, 06:39 PM
if (!isset($_POST['submit']))
//Form not submitted

if (isset($_POST['submit']))
//Form submitted
//Form not submitted.

Also you should listen to what Fou-Lou has said to you about checking for the submit button in your code. Internet explorer (v4, 5, 6, 7, and I believe v8) do not send the submit button when your users hits the return key when the cursor is inside a text box in your form. It will only send the button value when the button is physically clicked with the mouse. You're best checking for a hidden form field OR another element that your user is guaranteed to use.

08-21-2011, 06:39 PM
No it won't. Old versions of IE just don't submit it, even though its against the form standards.
You need to check the isset conditions of all the fields you are expecting to be there instead of the submit button.
As for your if branches, there is almost no limit on how far you want to go.

08-21-2011, 06:41 PM
So what exactly does the exclamation mark actually do?

Would I need to use a hidden field in my html?

For instance.

<input type='hidden' name='submit' />

08-21-2011, 06:57 PM
Yes that hidden field will do it but you'll have to give it a value - like this
<input type="hidden" name="submit" value="1">

The ! as Fou explained negates the value of whatever you're checking. In otherwords it makes it the opposite or NOT.

So.. true would become false and false would become true. In otherwords ! makes the if look for the opposite of true.

Thats about the only easy way I can explain it.

08-21-2011, 07:05 PM
Wow, thanks guys.

08-21-2011, 07:56 PM
silly question, indeed :) Okay, may not be so for newbies..
if(! ...)
else.. everything works!