Page 1 of 1

if elseif

Posted: Fri Aug 05, 2011 6:56 pm
by YoussefSiblini
Hi I am trying to use php to validate my form here is my code:

Code: Select all

<?php 

//Adding the user to the database and checking if he exist we give him an error

require_once ('includes/connect_to_mysql.php');
// Handle the form.
if (isset($_POST['submitted'])) { 
$firstname = preg_replace('#[^A-Za-z0-9]#i', '', $_POST["first_name"]); // filter everything but letters and numbers
$lastname = preg_replace('#[^A-Za-z0-9]#i', '', $_POST["last_name"]); // filter everything but letters and numbers
$email = $_POST["email"];
$password1 = $_POST["password1"];
$password2 = $_POST["password2"];
$humancheck = $_POST["humancheck"];

$email = stripslashes($email); 
$password1 = stripslashes($password1); 
$password2 = stripslashes($password2); 

$email = strip_tags($email); 
$password1 = strip_tags($password1); 
$password2 = strip_tags($password2); 

// query the person	
$ins = mysql_query("SELECT id FROM members WHERE Email='$email' LIMIT 1");
// ------- MAKE SURE PERSON EXISTS IN DATABASE ---------
$existCount = mysql_num_rows($ins); // count the row nums

// Error handling for missing data
if ((!$firstname) || (!$lastname) || (!$email) || (!$password1) || (!$password2) || (!$humancheck)) 
{ 
$errorMsg = 'ERROR: You did not submit the following required information:<br /><br />';

if(!$firstname){ 
$errorMsg .= ' * First Name<br />';
} 
if(!$lastname){ 
$errorMsg .= ' * Last Name.<br />';
} 
if(!$email){ 
$errorMsg .= ' * Email<br />'; 
}
if(!$password1){ 
$errorMsg .= ' * Password<br />'; 
} 
if(!$password2){ 
$errorMsg .= ' * Confirm Password<br />'; 
}
} else if($password1 != $password2) {
$errorMsg = 'ERROR: Your passwords fields below do not match<br />';
} else if($humancheck != "") {
$errorMsg = 'ERROR: The Human Check field must be cleared to be sure you are human<br />';	
} else if($existCount > 0){ 
$errorMsg = 'ERROR: Your Email address is already in use inside of our system. Please use another.<br />'; 
} 
else
{

// Add this person into the database now
$sql = mysql_query("INSERT INTO members (First_Name, Last_Name, Email, Password, Date) VALUES ('$firstname', '$lastname', '$email', '$password1' , NOW() )");
header("location: login.php"); 
exit();
}

}else {

$firstname ='';
$errorMsg = '';
$lastname = '';
$password1 = '';
$password1 = '';
$email = '';
};
?>

<!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=utf-8" />
<title>Untitled Document</title>

<link rel="stylesheet" type="text/css" href="../css/style.css" />
<link rel="stylesheet" href="../css/MenuMatic.css" type="text/css" media="screen" charset="utf-8" />
<!--[if lt IE 7]>
<link rel="stylesheet" href="../css/MenuMatic-ie6.css" type="text/css" media="screen" charset="utf-8" />
<![endif]-->
</head>

<body>
<!--main begin -->
<div id="main">

<!--header begin -->

<?php include_once "includes/header.php";?>

<!--header end -->

<!--content begin -->
<div id="content">
<h3>Please fill the form below to register:</h3>
<div style="color:red"><?php print "$errorMsg"; ?></div>
<form action="register.php" method="post" name="register_form">
<p><span style="width:150px; display:inline-block">First Name:</span><input type="text" id="register_firstname" name="first_name" value="<?php echo $firstname ?>" class="register_Text_Boxes" /></p>
<p><span style="width:150px; display:inline-block">Last Name:</span><input id="register_lastnamename" type="text" name="last_name" value="<?php print $lastname ?>" class="register_Text_Boxes" /></p>
<p><span style="width:150px; display:inline-block">Email Address:</span><input id="register_email" type="text" name="email" value="<?php echo $email ?>" class="register_Text_Boxes" /></p>
<p><span style="width:150px; display:inline-block">Password:</span><input id="register_password1" type="password" name="password1" class="register_Text_Boxes"/> </p>
<p><span style="width:150px; display:inline-block">Confirm Password:</span><input id="register_password2" type="password" name="password2" class="register_Text_Boxes" /></p>
<p><span style="width:150px; display:inline-block">Human Check:</span><input name="humancheck" type="text" id="humancheck" value="Please remove all of this text" class="register_Text_Boxes" /></p>


<div><span style="width:150px; display:inline-block"></span><input name="submit" type="submit" id="button" value="Register" /></div>
<input type="hidden" name="submitted" value="TRUE" />
</form>
</div>
<!--content end -->

</div>
<!--main end -->


<!--Footer begin -->

<?php include_once "../includes/footer.php";?>

<!--Footer end -->


<!-- Load the Mootools Framework -->
<script src="http://www.google.com/jsapi"></script><script>google.load("mootools", "1.2.1");</script>	

<!-- Load the MenuMatic Class -->
<script src="../js/MenuMatic_0.68.3.js" type="text/javascript" charset="utf-8"></script>

<!-- Create a MenuMatic Instance -->
<script type="text/javascript" >
window.addEvent('domready', function() {	
var myMenu = new MenuMatic();
});	
</script>

<!-- begin google tracking code -->
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-2180518-1");
pageTracker._initData();
pageTracker._trackPageview();
</script>
<!-- end google tracking code -->


</body>
</html>
Ok what I am getting is that when I fell all the fields in the form as I validated it I get this error:

ERROR: You did not submit the following required information:

Please help


Youssef

Re: if elseif

Posted: Fri Aug 05, 2011 8:55 pm
by Christopher
The instructions say to clear HumanCheck, so its value will be '' which is false. You need to change the code to || ($humancheck) /

Re: if elseif

Posted: Sat Aug 06, 2011 4:27 am
by YoussefSiblini
Thank you this worked for me :))