Form validation problem (newbie)

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

Post Reply
MarkMarkson
Forum Newbie
Posts: 3
Joined: Mon Oct 11, 2010 11:51 am

Form validation problem (newbie)

Post by MarkMarkson »

I have created a simple e-mail form consisting of three fields; name, e-mail address and the message itself.
The validation code works perfectly in Opera (v.10.62), but I hit a problem in both Firefox (v.3.6.10) and IE (v.7). The code checks to see if any fields are empty (all are required), and checks if the e-mail address is a valid address.

Code: Select all

                <?php
				if (isset($_POST['email']))
				{//if "email" is filled out, proceed
				
					$email = $_POST['email'] ;
					$name = $_POST['name'];
					$message = $_POST['message'] ;

					$emptyfields = FALSE;
				  	
					// Are there any empty fields?
					if (trim($email)=="" || trim($name)=="" || trim($message)=="")
					{
						$emptyfields = TRUE;
					}
					
					$validemail = validateaddress($email);
					
					if ($emptyfields==TRUE)
					{
						echo "<p style=\"color:red\">An error occured:<br />- All the fields have to be filled in.</p>";
						include("phpinclude/contact.php");
					}
					elseif ($validemail==FALSE)
					{
						echo "<p style=\"color:red\">An error occured:<br />- Invalid e-mail address.</p>";
						include("phpinclude/contact.php");
					}
				  	else
					{// Send e-mail
						$subject = "Web e-mail";
						mail_utf8("myaddress@somedomain.com", $subject,
						$message, "From: $name <$email>" );
						echo "Thank you.";
					}
				}
				else
				{// If the user has not yet submitted the form, display the form.
				?>
                    <p>
                    Please send me an e-mail using the following form.
                    </p>
                    
                <?php
					$email = "";
					$name = "";
					$message = "";
					include("phpinclude/contact.php");
				}
				?>

The contact.php file:

Code: Select all

<form method='post' action='index.php?page=contact>
        <table>
            <tr><td>Name:</td><td><input type="text" name="name" size="40" value="<?php echo $name ?>" /></td></tr>
            <tr><td>E-mail:</td><td><input type="text" name="email" size="40" value="<?php echo $email ?>" /></td></tr>
            <tr><td>Message:</td><td><textarea rows="10" cols="60" name="message"><?php echo $message ?></textarea></td></tr>
            <tr><td></td><td><input type="submit"></td></tr>
        </table>
</form>
The problem occurs when I repeatedly hit 'Submit' in Firefox and IE without even filling in any of the fields. The first few times it works, and the error message is displayed, but then it looks like the line "if (isset($_POST['email]))" suddenly returns false and the form is displayed as if 'Submit' hasn't been clicked. However as I mentioned, this isn't an issue in Opera.

Any help is appreciated.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Form validation problem (newbie)

Post by Jonah Bron »

You missed a quote on this line:

Code: Select all

if (isset($_POST['email]))
Needs to be this:

Code: Select all

if (isset($_POST['email']))
lavaeagle
Forum Newbie
Posts: 21
Joined: Thu Sep 09, 2010 1:41 pm

Re: Form validation problem (newbie)

Post by lavaeagle »

I want you to look into something that is a little more effective.

http://www.tizag.com/javascriptT/javascriptform.php

Now you don't have to do it exactly how it says in here but this is where I'd go with validation.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Form validation problem (newbie)

Post by Jonah Bron »

lavaeagle wrote:I want you to look into something that is a little more effective.

http://www.tizag.com/javascriptT/javascriptform.php

Now you don't have to do it exactly how it says in here but this is where I'd go with validation.
If you are suggesting that Javascript should be used instead of PHP for form validation, I could not hardly disagree more. JavaScript should NEVER (repeat, never) be relied upon for form validation. Yes, it can compliment server-side validation by giving the user immediate feedback, but EVERYTHING (repeat, everything) should be double-checked with PHP on the server.

You can use Javascript for form validation along with PHP (or another program on the server-side), but not as an alternative.
MarkMarkson
Forum Newbie
Posts: 3
Joined: Mon Oct 11, 2010 11:51 am

Re: Form validation problem (newbie)

Post by MarkMarkson »

Jonah Bron wrote:You missed a quote on this line:

Code: Select all

if (isset($_POST['email]))
Oooops, I happened to delete the quote when I wrote the first post. :oops: The actual php-file on the server doesn't have this error.

Could this problem be server-related? Sounds strange since it works flawlessly in Opera. I'm running this on a free hosting account at GoDaddy.com by the way.
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Re: Form validation problem (newbie)

Post by jason »

The problem occurs when I repeatedly hit 'Submit' in Firefox and IE
What do you mean here by repeatedly? Are you spamming Submit or just hitting it once, then showing the results, and then clicking the button again?
MarkMarkson
Forum Newbie
Posts: 3
Joined: Mon Oct 11, 2010 11:51 am

Re: Form validation problem (newbie)

Post by MarkMarkson »

I click the button, wait for the page to finish loading, then click the button again. After clicking the button 2-3 times, the form appears as if I hadn't clicked submit (the code in the last else-statement executes apparently).
lavaeagle
Forum Newbie
Posts: 21
Joined: Thu Sep 09, 2010 1:41 pm

Re: Form validation problem (newbie)

Post by lavaeagle »

Jonah Bron wrote:
lavaeagle wrote:I want you to look into something that is a little more effective.

http://www.tizag.com/javascriptT/javascriptform.php

Now you don't have to do it exactly how it says in here but this is where I'd go with validation.
If you are suggesting that Javascript should be used instead of PHP for form validation, I could not hardly disagree more. JavaScript should NEVER (repeat, never) be relied upon for form validation. Yes, it can compliment server-side validation by giving the user immediate feedback, but EVERYTHING (repeat, everything) should be double-checked with PHP on the server.

You can use Javascript for form validation along with PHP (or another program on the server-side), but not as an alternative.
Whoa there cowboy no where did I say "Don't use PHP because javascript is better".

For UI design and cleanliness use Javascript as the first form of validation which avoids the page reloads which is easier on the eyes.
Of course use PHP to securely form validate.

You will see more professional websites using javascript to validate first because it looks much better.

Code: Select all


/* add your mysql_escap_strings and stripslashes */

$msg = 0;
if($_POST['email'] == ''){ $msg = 1; } ;
if($_POST['message'] == ''){ $msg = 2; } ;
if($_POST['name'] == ''){ $msg = 3; } ;

if($msg != 0){ header('contact.php?msg='.$msg);

User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Form validation problem (newbie)

Post by Jonah Bron »

lavaeagle wrote:Whoa there cowboy no where did I say "Don't use PHP because javascript is better"
Good. That's why I put in "If you are suggesting that Javascript should be used instead of PHP ..."; just in case. I wasn't yelling at you, I was just putting emphasis on those words to make it VERY (repeat, very) clear to the original poster. :)
Post Reply