Page 1 of 1

form validation - Multiple IF conditions....

Posted: Sun Dec 28, 2003 11:48 am
by volka
with permission of the original poster moved from pm to this forum.
Hello:

I am VERY new to PHP and slowly learning. Here is a sample of my code:

Code: Select all

<?php
	If ($btnSUB) {               
		If (isset($fname) && $fname == "")
		{
			echo '<TR>';
			echo '<TD CLASS=alerts1 COLSPAN="2">';
			alertfunc();
			echo '</TD>';            
			echo '</TR>';
		}            
		If (isset($lname) && $lname == "")
		{
			echo '<TR>';            
			echo '<TD CLASS=alerts1 COLSPAN="2">';   
			alertfunc2();            
			echo '</TD>';            
			echo '</TR>';            
		}
		If (isset($email) && $email == "")  
		{    
			echo '<TR>';
			echo '<TD CLASS=alerts1 COLSPAN="2">';
			alertfunc3();
			echo '</TD>';
			echo '</TR>';
		}
		If ($passwd1 != $passwd2)
		{
			echo '<TR>';
			echo '<TD CLASS=alerts1 COLSPAN="2">';
			alertfuncconfirm();
			echo '</TD>';
			echo '</TR>';
		}
	}
	Else
	{
		$upperfname = $fname;
		$upperfname = ucfirst($upperfname);
		$upperlname = $lname;
		$upperlname = ucfirst($upperlname);         
		INCLUDE "dbconnect.php";
		$sql =
			"INSERT INTO useraccount
			(FirstName,LastName,EmailAddress,Password,Timestamp,UserID)
			VALUES
			('$upperfname','$upperlname','$email','$passwd1','$tstamp','$uid')";

		$result = mysql_query($sql);
		echo "<SCRIPT>window.location="index.php";</SCRIPT>";
	}
?>
[slightly reformatted]

Basically what I am "trying" to do is, I have a form with FIRST NAME, LAST NAME, EMAIL, and PASSWORD. I want to be able to say "if firstname is null then alert "sorry... you must enter a first name". Now, if firstname AND lastname is null, I want to be able to show two separate alerts at the same time like:

* Please enter your first name
* Please enter your last name

So.... for each field that is null.... I want to display a message. I am having issues with my nested IF statements..... HELP!

Thank you,


nonborn0

Posted: Sun Dec 28, 2003 11:58 am
by volka
the main problem is probably solved already. You forgot to mark the inner if-blocks with { }
if(isset($fname) && $fname == "")
only if this variable is set and is == "" the script will enter the if-body. What if the variable isn't set at all? Might be better to use

Code: Select all

if(!isset($fname) || strlen($fname))
(if it is not set at all or contains no data)

also make sure viewtopic.php?t=511 doesn't apply to your version of php (or change the script accordingly).

You should also always check user input data before using with a sql-statement
see also:
http://www.sitepoint.com/article/794 (although it seems to be for asp)
http://de2.php.net/mysql_escape_string

The way I do

Posted: Sun Dec 28, 2003 10:13 pm
by mwong
It's probably a long way to do it...but when I did it....it was the only way I knew how to do it. Haven't changed it since.. You echo the $error at the end.

Code: Select all

if((!$first_name) || (!$last_name) || (!$email_address) || (!$username) || (!$phone) || (!$street) || (!$city) || (!$state) || (!$zip))&#123; 

    $error = 'You did not submit the following required information! <br />'; 

    if(!$first_name)&#123; 

        $error .="&nbsp;&nbsp;&nbsp;&nbsp;&#8226;&nbsp;First Name<br />"; 

    &#125; 

    if(!$last_name)&#123; 

        $error .= "&nbsp;&nbsp;&nbsp;&nbsp;&#8226;&nbsp;Last Name<br />"; 

    &#125; 

    if(!$email_address)&#123; 

         $error .= "&nbsp;&nbsp;&nbsp;&nbsp;&#8226;&nbsp;Email Address<br />"; 

    &#125; 

    if(!$username)&#123; 

         $error .= "&nbsp;&nbsp;&nbsp;&nbsp;&#8226;&nbsp;Username<br />"; 

    &#125; 
	
	if(!$phone)&#123; 

         $error .= "&nbsp;&nbsp;&nbsp;&nbsp;&#8226;&nbsp;Phone<br />"; 

    &#125; 
	
	if(!$street)&#123; 

         $error .= "&nbsp;&nbsp;&nbsp;&nbsp;&#8226;&nbsp;Street<br />"; 

    &#125; 
	
	if(!$city)&#123; 

         $error .= "&nbsp;&nbsp;&nbsp;&nbsp;&#8226;&nbsp;City<br />"; 

    &#125; 
	
	if(!$state)&#123; 

         $error .= "&nbsp;&nbsp;&nbsp;&nbsp;&#8226;&nbsp;State<br />"; 

    &#125;
	
	if(!$zip)&#123; 

         $error .= "&nbsp;&nbsp;&nbsp;&nbsp;&#8226;&nbsp;Zip<br />"; 

    &#125;

Posted: Sun Dec 28, 2003 11:43 pm
by Weirdan
I'd suggest the following code:

Code: Select all

$field_names=array(
  "first_name" => "First Name",
  "last_name" => "Last Name",
  "email_address" => "Email Address",
  "username" => "Username",
  "phone" => "Phone",
  "street" => "Street",
  "city" => "City",
  "state" => "State",
  "zip" => "Zip"
);
foreach($field_names as $field=>$name)
  if(!isset($_POST[$field]))
    $errors.="    &#8226; ".$name."<br />";
if(strlen($errors)) 
  $errors='You did not submit the following required information! <br />'.$errors;
I find it shorter and more readable...

Posted: Mon Dec 29, 2003 12:59 am
by mwong
:D Yep!! that does look alot better. Thanks!
Weirdan wrote:I'd suggest the following code:

Code: Select all

$field_names=array(
  "first_name" => "First Name",
  "last_name" => "Last Name",
  "email_address" => "Email Address",
  "username" => "Username",
  "phone" => "Phone",
  "street" => "Street",
  "city" => "City",
  "state" => "State",
  "zip" => "Zip"
);
foreach($field_names as $field=>$name)
  if(!isset($_POST[$field]))
    $errors.="    &#8226; ".$name."<br />";
if(strlen($errors)) 
  $errors='You did not submit the following required information! <br />'.$errors;
I find it shorter and more readable...