Hello:
I am VERY new to PHP and slowly learning. Here is a sample of my code:
[slightly reformatted]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>"; } ?>
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
form validation - Multiple IF conditions....
Moderator: General Moderators
form validation - Multiple IF conditions....
with permission of the original poster moved from pm to this forum.
the main problem is probably solved already. You forgot to mark the inner if-blocks with { }
(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
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 useif(isset($fname) && $fname == "")
Code: Select all
if(!isset($fname) || strlen($fname))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
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)){
$error = 'You did not submit the following required information! <br />';
if(!$first_name){
$error .=" • First Name<br />";
}
if(!$last_name){
$error .= " • Last Name<br />";
}
if(!$email_address){
$error .= " • Email Address<br />";
}
if(!$username){
$error .= " • Username<br />";
}
if(!$phone){
$error .= " • Phone<br />";
}
if(!$street){
$error .= " • Street<br />";
}
if(!$city){
$error .= " • City<br />";
}
if(!$state){
$error .= " • State<br />";
}
if(!$zip){
$error .= " • Zip<br />";
}I'd suggest the following code:
I find it shorter and more readable...
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.=" • ".$name."<br />";
if(strlen($errors))
$errors='You did not submit the following required information! <br />'.$errors;Weirdan wrote:I'd suggest the following code:I find it shorter and more readable...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.=" • ".$name."<br />"; if(strlen($errors)) $errors='You did not submit the following required information! <br />'.$errors;