Check if form is submitted
Moderator: General Moderators
Check if form is submitted
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
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
- launchcode
- Forum Contributor
- Posts: 401
- Joined: Tue May 11, 2004 7:32 pm
- Location: UK
- Contact:
Take one of your form fields that must ALWAYS be set (like the name of the submit button or something) and just do:
Or a variation on the above.
Code: Select all
if (isset($_POST['formthing']))
{
// do validation here
}- launchcode
- Forum Contributor
- Posts: 401
- Joined: Tue May 11, 2004 7:32 pm
- Location: UK
- Contact:
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:
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 
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";
}
}
?>- launchcode
- Forum Contributor
- Posts: 401
- Joined: Tue May 11, 2004 7:32 pm
- Location: UK
- Contact:
-
magicrobotmonkey
- Forum Regular
- Posts: 888
- Joined: Sun Mar 21, 2004 1:09 pm
- Location: Cambridge, MA
Ahhh ok.
So the following code is really not that great unless you disable the use of the 'enter' key!
Hmm - I have now come across several other people suggesting the above code for checking for form submittal.
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']))-
magicrobotmonkey
- Forum Regular
- Posts: 888
- Joined: Sun Mar 21, 2004 1:09 pm
- Location: Cambridge, MA
-
magicrobotmonkey
- Forum Regular
- Posts: 888
- Joined: Sun Mar 21, 2004 1:09 pm
- Location: Cambridge, MA
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
If I click 'submit' I get
Any thoughts?
When I hit the Enter key the output from print_r gives
Code: Select all
Array
(
їEmail] => rubbishemail
)Code: Select all
Array
(
їEmail] => rubbishemail
їSubmit] => Submit
)You can, you need to use javascript to capture the key press, then submit the formmagicrobotmonkey 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!
Mark