Page 1 of 1

Form validation - am I doing this right?

Posted: Mon Oct 30, 2006 7:53 am
by jm999
I'm trying to make sure all of the fields of a form are filled out. Here is my code:

Code: Select all

if ($fname == '' or $lname == '' or $email == '' or $cemail == '' or $guests == '' or $pswd == '' or $cpswd == ''); {
echo $errmsg3; }
I keep getting the error message even if all of the fields are filled out. Does anyone know what I'm doing wrong? Thanks for reading.

Posted: Mon Oct 30, 2006 8:00 am
by aaronhall
Use '||' instead of 'or'. If you have register_globals turned off, you need to use the $_POST or $_GET superglobal depending on your form's submission method, e.g. $_POST['fname'].

You can use the empty function to clean it up a little bit:

Code: Select all

<?
if(empty($_POST['fname']) || empty($_POST['lname']) || ...
?>

Posted: Mon Oct 30, 2006 8:32 am
by jm999
I took your advice and replaced the original code with:

Code: Select all

//Check to make sure all fields were filled out
if(empty($_POST['firstname']) || empty($_POST['lastname']) || empty($_POST['email']) || empty($_POST['cemail']) || empty($_POST['guests']) || empty($_POST['pswd']) || empty($_POST['cpswd'])); 
	{
	echo $errmsg3;
	}
But the error message is still showing up when all fields are filled in.

Posted: Mon Oct 30, 2006 8:47 am
by aaronhall
Print out all of the variables that you are checking, and see which one is coming up empty. Go back and see if you can find why it's empty.

Posted: Mon Oct 30, 2006 9:04 am
by kettle_drum
And remember that variable names are case sensitive - so check the form field names very carefully.

Posted: Mon Oct 30, 2006 9:20 am
by jm999
Everything prints out fine, even without using $_POST. But I still get the error that fields were left blank.

I also put in

Code: Select all

if ($pswd != $cpswd) {echo $errmsg1;}
This doesn't work either. Is it my syntax? They match but I still get an error. I turned register_globals on in my php.ini. I'm using php 5. This is driving me nuts.

Posted: Mon Oct 30, 2006 9:27 am
by jm999
JayBird | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Here is the full code for register.php :

[syntax="html"]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>Well Heeled Society ~ Who says you have to be married to get great gifts? </title>
<link rel="stylesheet" type="text/css" href="css/whs.css">
</head>
<body>

<?php require ("connect.php"); ?>

<!-- Outer Container / Centers Content -->
<div id="wrap">
 
  <div id="header"></div>
  <div id="navigation"><?php include ('navigation.php'); ?></div>
  
<!-- Main Content Area  -->
  <div id="main-content">
   

	
	<div id="searchwrap"><div class="searchform1"><form name="search" action="search.php" method="post">Registry Search : <input type="text" name="terms" size="15">&nbsp;<input type="submit" value="Search" class="button"></form></div></div>
   
   
    <div id="main">
	<div id="regform"><div class="regformtext"><p>&nbsp;</p><br /><br />
	<form name="register" action="registration.php" method="post">
	First Name : <input type="text" name="firstname" size="15" /><br /><br />
	Last Name &nbsp;: <input type="text" name="lastname" size="15" /><br /><br />
	E-mail Address : <input type="text" name="email" size="15" /><br /><br />
	Confirm e-mail&nbsp;: <input type="text" name="cemail" size="15" /><br /><br />
	Guest Email addresses : <input type="text" name="guests" size="25"/><br />
	<span class="smallprint">(<a href="privacy.php">read our privacy policy</a>)&nbsp;(please seperate each address with a comma)</span><br /><br />
	Choose a password : <input type="text" name="pswd" size="15" /><br /><br />
	Confirm password : <input type="text" name="cpswd" size="15" /><br /><br />
	
	<span align="center"><input type="submit" value="submit" class="button" /></span></form></div></div>
	
	<p>&nbsp;</p>
	
<p align="center">  <script src="http://www.joinred.com/hookup/assets.asp?ori=1026&sel=ba5"></script> </p>
    </div>
      
     <!-- Begin #footer :: bottom area -->
     <div id="footer">
      <?php include('footer.php'); ?>
      </p>
     </div>
  

  
  </div>
  <!-- End #main-content -->
  
	<div id="main-bot"></div>  
 
</div>
</body>
</html>
Heres the registration.php :[/syntax]

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Well Heeled Society ~ Who says you have to be married to get great gifts?</title>
<link rel="stylesheet" type="text/css" href="css/whs.css" />
<?php require('connect.php'); ?>
<?php
$fname = $_POST['firstname'];
$lname = $_POST['lastname'];
$email = $_POST['email'];
$cemail = $_POST['cemail'];
$guests = $_POST['guests'];
$pswd = $_POST['pswd'];
$cpswd = $_POST['cpswd'];
$errmsg1 = ('Your passwords don\'t match. Please go back and resubmit the registration form.');
$errmsg2 = ('That email address is already linked to an account!');
$errmsg3 = ('You left some fields blank. Please go back and resubmit the registration form.');
?>	

</head>
<body>

<div id="wrap">
 
  <div id="header"></div>
  <div id="navigation"><?php include ('navigation.php'); ?></div>
  
<!-- Main Content Area  -->
  <div id="main-content">
   

	
	<div id="searchwrap"><div class="searchform1"><form name="search" action="search.php" method="post">Registry Search : <input type="text" name="terms" size="15">&nbsp;<input type="submit" value="Search" class="button"></form></div></div>
   
   
    <div id="main">


<?php echo $fname; ?><br />
<?php echo $lname; ?><br />
<?php echo $email; ?><br />
<?php echo $cemail; ?><br />
<?php echo $guests; ?><br />
<?php echo $pswd; ?><br />
<?php echo $cpswd; ?><br />

<?php
//Confirm the password
if ($pswd != $cpswd) {
	echo $errmsg1;  }
	

	
//Check to make sure all fields were filled out
if(empty($_POST['firstname']) || empty($_POST['lastname']) || empty($_POST['email']) || empty($_POST['cemail']) || empty($_POST['guests']) || empty($_POST['pswd']) || empty($_POST['cpswd'])); 
	{
	echo $errmsg3;
	}
	?>


<p>&nbsp;</p>
	
	<p align="center">  <script src="http://www.joinred.com/hookup/assets.asp?ori=1026&sel=ba5"></script> </p>
	
</div>
<div id="footer"><?php include ('footer.php'); ?></div>

</div>

</body>
</html>
I know my code is super sloppy. Sorry about that. I've just been rearranging things around so much that its getting jumbled.


JayBird | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Mon Oct 30, 2006 9:30 am
by aaronhall
It's not recommended that you enable register_globals for security reasons. Stick the following code in the script that you have the validation code in, and post the results here.

Code: Select all

<?
print_r($_POST);
?>

Posted: Mon Oct 30, 2006 9:32 am
by aaronhall

Code: Select all

if(empty($_POST['firstname']) || empty($_POST['lastname']) || empty($_POST['email']) || empty($_POST['cemail']) || empty($_POST['guests']) || empty($_POST['pswd']) || empty($_POST['cpswd']));
   {
   echo $errmsg3;
   }
should be

Code: Select all

if(empty($_POST['firstname']) || empty($_POST['lastname']) || empty($_POST['email']) || empty($_POST['cemail']) || empty($_POST['guests']) || empty($_POST['pswd']) || empty($_POST['cpswd']))
   {
   echo $errmsg3;
   } 
you had a semi-colon after the if statement

Posted: Mon Oct 30, 2006 9:47 am
by jm999
The form works now. Apparently that semi-colon was the wrench in the gears. Thanks for helping me out with that. I knew it had to be something very simple :oops: .

Posted: Mon Oct 30, 2006 9:50 am
by aaronhall
It usually is, and takes the longest to find. That's why I like ZDE -- puts red squiggly lines under places where I did something dumb.

Posted: Sat Jan 06, 2007 2:52 pm
by feinstimmer
aaronhall, what is ZDE?I am also beginner in PHP and i can use every help which i can get. :oops:

Posted: Sat Jan 06, 2007 3:46 pm
by aaronhall
ZDE is a development environment (editor, debugger and more): http://www.zend.com/products/zend_studio -- I think they'll let you download a trial version. It is a little on the expensive side, though. A lot of others are listed here, including Komodoand Eclipse.

Posted: Sat Jan 06, 2007 3:52 pm
by feinstimmer
thanks aaronhall, i'll take a look!! :)

Posted: Sat Jan 06, 2007 4:51 pm
by aaronhall
:)