Page 1 of 2
Check if form is submitted
Posted: Thu May 13, 2004 7:16 am
by hairyjim
Hi,
I have had a search through the forums but I gotta say the forum search is poor.
Anyways Im not here to rant about forum searches.
I have a form, and when it is submitted (to itself PHP_SELF) I want to run a validation routine to check certain fields for content etc.
The snag I have is when the form is first loaded it does the validation and displays the errors! I realise this is because I have not put any code in to check if the form was submitted. If the form was not submitted then don't do any validation, if the form has being submitted then perform the validation.
Could someone please point me in the direction where I can find how to do this properly.
Cheers
Jim
Posted: Thu May 13, 2004 7:27 am
by launchcode
Take one of your form fields that must ALWAYS be set (like the name of the submit button or something) and just do:
Code: Select all
if (isset($_POST['formthing']))
{
// do validation here
}
Or a variation on the above.
Posted: Thu May 13, 2004 7:39 am
by hairyjim
So easy - why didn't I think of that. BAH!
Cheers. Help most appreciated.
Posted: Thu May 13, 2004 7:54 am
by hairyjim
Ahhh - this is actually not as robust as you may think.
If I were to do as suggested, but I hit the enter key it does not work!
Is there anything more bullet proof?
Jim
Posted: Thu May 13, 2004 8:00 am
by launchcode
Like I said - make the $_POST value you are checking your Submit button!

Posted: Thu May 13, 2004 8:16 am
by hairyjim
I did. If you run the following code it does as expected. Form first loaded there is no validation or errors performed/outputted.
Enter an email either right or wrong, then hit 'return' key. Voila - still no validation or errors output.
I bet I'm just been a bit thick.
Here is my code:
Code: Select all
<form action="<? $_SERVERї'PHP_SELF'] ?>" method="post" name="Contact">
<table width="300" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="225">Email address: </td>
<td width="25"><input name="Email" type="text" id="Email" size="30"></td>
</tr>
<tr>
<td> </td>
<td>
<div align="center">
<input type="submit" name="Submit" value="Submit">
<input type="reset" name="Reset" value="Reset">
</div></td>
</tr>
</table>
</form>
<?
if (isset($_POSTї'Submit']))
{
include "clsMyClass.php";
// Initialize the object giving it a named variable
$myclass = &New MyClass;
// Assign a value to $email residing in class
$myclass->email = $_POSTї'Email'];
// Run check email function inside class
$check_email = $myclass->check_email();
// Return errors
if(!$check_email){
echo "The email does not appear to be valid!";
} else {
echo "The email seems valid";
}
}
?>
This is me 'teaching myself' how to write re-usable code using classes, so if you realise I am doing other things completly wrong let me know too

Posted: Thu May 13, 2004 8:21 am
by launchcode
Dump out the value of $_POST:
<?php
print_r($_POST);
?>
write it in PRE tags for easy reading - now you can see exactly what gets submitted when you do/don't fill anything in.
Posted: Thu May 13, 2004 8:31 am
by magicrobotmonkey
if you hit enter instead of clicking the submit button, i believe it isnt posted - so either check for a field that's required or put in a hidden form field and check that
Posted: Thu May 13, 2004 8:38 am
by hairyjim
Ahhh ok.
So the following code is really not that great unless you disable the use of the 'enter' key!
Code: Select all
if (isset($_POSTї'Submit']))
Hmm - I have now come across several other people suggesting the above code for checking for form submittal.
Posted: Thu May 13, 2004 8:41 am
by tim
my .02 cents
send the data across to a different page for validation, I dont like $PHP_SELF.
why dont u post some code
Posted: Thu May 13, 2004 8:44 am
by magicrobotmonkey
ahh ok my testing has confirmed that i was wrong - at least in IE and Firefox
Posted: Thu May 13, 2004 8:46 am
by tim
yeah the enter key is the same as literally clicking the submit button.
i find it alot better to click enter after filling a form then to hobble around with my mouse, lol!
Posted: Thu May 13, 2004 8:49 am
by magicrobotmonkey
yea i know me too, i don't know why i thought that. is it possible to have a form with no submit button that you can hit enter to submit... I guess there's only one way to find out!
And the answer is... NO!
Posted: Thu May 13, 2004 8:57 am
by hairyjim
So why when I hit the enter key does my form not validate.
When I hit the Enter key the output from print_r gives
Code: Select all
Array
(
їEmail] => rubbishemail
)
If I click 'submit' I get
Code: Select all
Array
(
їEmail] => rubbishemail
їSubmit] => Submit
)
Any thoughts?
Posted: Thu May 13, 2004 8:59 am
by JayBird
magicrobotmonkey wrote:yea i know me too, i don't know why i thought that. is it possible to have a form with no submit button that you can hit enter to submit... I guess there's only one way to find out!
And the answer is... NO!
You can, you need to use javascript to capture the key press, then submit the form
Mark