Check if form is submitted

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

hairyjim
Forum Contributor
Posts: 219
Joined: Wed Nov 13, 2002 9:04 am
Location: Warwickshire, UK

Check if form is submitted

Post 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
User avatar
launchcode
Forum Contributor
Posts: 401
Joined: Tue May 11, 2004 7:32 pm
Location: UK
Contact:

Post 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.
hairyjim
Forum Contributor
Posts: 219
Joined: Wed Nov 13, 2002 9:04 am
Location: Warwickshire, UK

Post by hairyjim »

:D

So easy - why didn't I think of that. BAH!

Cheers. Help most appreciated.
hairyjim
Forum Contributor
Posts: 219
Joined: Wed Nov 13, 2002 9:04 am
Location: Warwickshire, UK

Post 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
User avatar
launchcode
Forum Contributor
Posts: 401
Joined: Tue May 11, 2004 7:32 pm
Location: UK
Contact:

Post by launchcode »

Like I said - make the $_POST value you are checking your Submit button! ;)
hairyjim
Forum Contributor
Posts: 219
Joined: Wed Nov 13, 2002 9:04 am
Location: Warwickshire, UK

Post 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&#1111;'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>&nbsp;</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&#1111;'Submit']))
&#123;
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&#1111;'Email'];

// Run check email function inside class
$check_email = $myclass->check_email();

// Return errors
if(!$check_email)&#123;
	echo "The email does not appear to be valid!";
&#125; else &#123;
	echo "The email seems valid";
&#125;
&#125;
?>
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 :wink:
User avatar
launchcode
Forum Contributor
Posts: 401
Joined: Tue May 11, 2004 7:32 pm
Location: UK
Contact:

Post 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.
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post 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
hairyjim
Forum Contributor
Posts: 219
Joined: Wed Nov 13, 2002 9:04 am
Location: Warwickshire, UK

Post 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&#1111;'Submit']))
Hmm - I have now come across several other people suggesting the above code for checking for form submittal.
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post 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
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

ahh ok my testing has confirmed that i was wrong - at least in IE and Firefox
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post 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!
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post 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!
hairyjim
Forum Contributor
Posts: 219
Joined: Wed Nov 13, 2002 9:04 am
Location: Warwickshire, UK

Post 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
(
    &#1111;Email] => rubbishemail
)
If I click 'submit' I get

Code: Select all

Array
(
    &#1111;Email] => rubbishemail
    &#1111;Submit] => Submit
)
Any thoughts?
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post 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
Post Reply