Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

if elseif

Featured Replies

Hi I am trying to use php to validate my form here is my code:

<?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

For sanitization you should always use htmlentities.

 

For validation you should use the built in PHP email validator, Validate Filters.

 

You could also set a minimum and maximum allowed number of characters and types of characters which are allowed to be used such as letters, hyphen and apostrophe using the Preg_Match.

I think you should end your if statement like following :

 

// Error handling for missing data

if ((!$firstname) || (!$lastname) || (!$email) || (!$password1) || (!$password2) || (!$humancheck))

{

//your validation start here

 

$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 />';

}

 

// ends your validation

 

} // here you should end your if statement and then

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();

}

 

 

try this

  • Author

I think you should end your if statement like following :

 

// Error handling for missing data

if ((!$firstname) || (!$lastname) || (!$email) || (!$password1) || (!$password2) || (!$humancheck))

{

//your validation start here

 

$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 />';

}

 

// ends your validation

 

} // here you should end your if statement and then

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();

}

 

 

try this

 

 

Thank you, this worked for me :)

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.