PHP Code Error. Can't find my logic mistake - php

Ok. So this is my code below. I'm trying to follow a tutorial on webtuts about validating email. But my sample is not working out. It is supposed to alert the user that it has entered an invalid email. So what my mate did is he created the "show_warning" jquery function to allow me to display my $msg. But it doesn't work. Is my logic wrong?.
<?php
if(isset($_POST['username']) && !empty($_POST['username']) AND
isset($_POST['password']) && !empty($_POST['password']) AND
isset($_POST['email']) && !empty($_POST['email']) AND
isset($_POST['role_id']) && !empty($_POST['role_id']))
{
$username = ($_POST['username']);
$password = ($_POST['password']);
$email = ($_POST['email']);
$role_id = ($_POST['role_id']);
if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email))
{
$msg = 'The email you entered is invalid. Please try again.';
}
else
{
$msg = 'Your account has been made, <br /> please verify by clicking the activation link in your email.';
}
}
?>
======================================
<div id="main-content">
<div id="create-user">
<h1>Create User</h1>
<form action="" method="post">
<table id="userform" width="600px" border=0>
<tr>
<td><label for="username">Username</label>
<input type="text" id="username" name="username" /></td>
<td><label for="password">Password</label>
<input type="text" id="password" name="password" /></td>
</tr>
<tr>
<td><label for="email">Email</label>
<input type="text" id="email" name="email" /></td>
<td><label for="role_id">Role</label>
<select>
<?php $roles = load_roles() ;?>
<?php foreach ($roles as $role): ?>
<option value='<?php echo $role['role_id']; ?>'><?php echo $role['role']; ?></option>
<?php endforeach; ?>
</select></td>
</tr>
<tr>
<td><input type="submit" id="submit" name="save_user" value="Create User"></td>
</tr>
</table>
</form>
</div>
</div>

for validating email php provides
$email="test#gmail.com" //your email to validate here
if(filter_var($email, FILTER_VALIDATE_EMAIL)){
echo "E-mail is valid";
}
else
{
echo "E-mail is not valid";
}
and you must not use eregi. you can use preg_match()
for more validation function follow this link
http://php.net/manual/en/filter.filters.validate.php

eregi() function has been DEPRECATED as of PHP 5.3.0. Relying on this feature is highly discouraged.
so its would be actual reason
AND preg_match() with the i (PCRE_CASELESS) modifier is the suggested alternative.
as in answer of Yadav Chetan use FILTER_VALIDATE_EMAIL instead those regx

if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email))
{
echo $msg = 'The email you entered is invalid. Please try again.';
}
else
{
echo $msg = 'Your account has been made, <br /> please verify by clicking the activation link in your email.';
}

Related

Associative Array check if login combination is correct

This is my very first post and question on this website. I'm working on my school assignment now and I have to check if the login credentials are the same as the ones that are listed in my associative array. I have searched everywhere but I couldn't find an answer. Can someone please tell me how to do this?
This is my PHP code:
// Create associative array
$loginCombinations = array("Lucas"=>"lucas3284", "Bob"=>"bob9584", "Frits"=>"frits1842", "Kees"=>"kees1394", "Sjakie"=>"sjakie1953", "Bas"=>"bas6382", "Peter"=>"peter2391", "Robbie"=>"robbie1289", "Jan"=>"jan1462", "Tim"=>"tim9324");
// Create message (login succesful / login failed)
$message = "";
// Create foreach loop
foreach($loginCombinations as $username => $password)
{
}
and this is my HTML code:
<form action="login.php" method="get">
<table>
<tr>
<td>
</td>
<td>
<?php
echo $message;
?>
</td>
</tr>
<tr>
<td>
<label for="username">username</label>
</td>
<td>
<input type="text" id="username" name="username">
</td>
</tr>
<tr>
<td>
<label for="password">password</label>
</td>
<td>
<input type="password" id="password" name="password">
</td>
</tr>
<tr>
<td>
</td>
<td>
<input type="submit">
</td>
</tr>
</table>
</form>
First, change GET method to POST as <form action="login.php" method="POST"> to send the data in request payload rather than as GET parameters for security.
So, you would put if condition in the foreach loop to check and echo success message accordingly.
<?php
$found = false;
foreach($loginCombinations as $username => $password){
if($_POST['username'] == $username && $_POST['password'] == $password){
echo "Yes, user found!!";
$found = true;
break;
}
}
if(!$found){
echo "No user found!!";
}
Update:
Add a name attribute to your submit button say submit like <input type="submit" name="submit">. Now, you will need to add an additional if condition to check if data was actually posted.
<?php
if(isset($_POST['submit'])){
$found = false;
foreach($loginCombinations as $username => $password){
if($_POST['username'] == $username && $_POST['password'] == $password){
echo "Yes, user found!!";
$found = true;
break;
}
}
if(!$found){
echo "No user found!!";
}
}
Update #2:
As pointed out by #Nigel Ren, you can simply do an isset check.
if(isset($loginCombinations[$_POST['username']],$loginCombinations[$_POST['password']])){
echo "user found";
}else{
echo "user not found";
}
As your array is indexed by the user name, there is no need to do a loop. First check if the user name element is set and then check the password for a match...
$userName = $_GET['username'] ?? '';
$message = "";
if ( isset($loginCombinations[$userName]) &&
$loginCombinations[$userName] === $_GET['password']) {
$message = "user login correct";
}
else {
$message = "user login incorrect";
}

email validator is not working PHP [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
i am new in php and i am working on a login and registration form . I created a function to check if the email address is valid or not even though i did't get any errors but it don't seem to work. It would be a great help if anyone here could take a look at my code.
<?php
$firstname="";
$lastname="";
$email="";
$password="";
$confirm_password="";
$error= array("firstname"=>"",
"lastname" =>"",
"email" =>"",
"password" =>"",
"confirm_password"=>"" );
function correct_email(){
if(!empty($email)){
if(filter_var($email,FILTER_VALIDATE_EMAIL)){
return true;
}else{
echo"your email address is not valid";
}
}
return false;
}
function correct_password(){
global $error;
if(empty($error['password'])){
if(strcmp($_POST['password'],$_POST['confirm_password'])==0){
return true;
}else{
$error['password']="Password didnot match";
$error['confirm_password']="Password didnot match";
}
}
return false;
}
function validate(){
global $error;
$valid= true;
foreach($_POST as $key=>$value){
if(empty($value)){
$valid=false;
$error[$key]="This field is blank";
}
}
return correct_password() && correct_email() && $valid;
}
if($_SERVER['REQUEST_METHOD']=="POST"){
$firstname=$_POST['firstname'];
$lastname=$_POST['lastname'];
$email=$_POST['email'];
$password=$_POST['password'];
$confirm_password=$_POST['confirm_password'];
if(validate()){
echo "registration succesful";
die;
}
else{
echo "couldnot register";
}
}
?>
<html>
<head>
<title>Registration</title>
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
<h1>New Memebers Registration</h1>
<form method="post" action="register.php">
<table>
<tr>
<td>Firstname:</td>
<td><input type="text" name="firstname" value="<?= $firstname ?>">
<?= $error['firstname'] ?> </td>
</tr>
<tr>
<td>Lastname:</td>
<td><input type="text" name="lastname" value="<?= $lastname ?>">
<?= $ error['lastname'] ?> </td>
</tr>
<tr>
<td>Email:</td>
<td><input type="text" name="email" value="<?= $email ?>">
<?= $error['email'] ?> </td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" name="password" value="<?= $password ?>">
<?= $error['password'] ?> </td>
</tr>
<tr>
<td>Confirm Password:</td>
<td><input type="password" name="confirm_password"
value="<?= $confirm_password ?>">
<?= $error['confirm_password'] ?> </td>
</tr>
<tr>
<td colspan="2" class="pullright">
<input type="submit" value="Register"> </td>
</tr>
</table>
</form>
</body>
</html>
You don't pass $email to your function so it is not available within your function due to scope. See variable scope to learn more about this.
filter_var() will return false if the email address is blank so you don't need to check that.
You should not echo out anything from your function. Just return true or false and let the code do the error handling.
But if you insist on setting error message in your code you are being inconsistent in your code as you echo an error message here instead of capturing it in your $error array like elsewhere in your code.
You can return true by default and only return false on error which shortens up your code a bit.
Your function:
correct_email($email) {
if (!filter_var($email,FILTER_VALIDATE_EMAIL)) {
$error['email'] = "your email address is not valid";
return false;
}
return true;
}
Calling it:
return correct_password() && correct_email($email) && $valid;
I recommend removing any reference to global and pass all needed variables as parameters as using global is considered a bad programming practice.
the variable is outside the function scope.
you need to define the function like:
function correct_email($email)...
when you call it parse email first to validate() then to correct_email:
correct_email($email)
or use the global
correct_email($_POST['email'])

How to show error messages in HTML page in PHP?

I have following login form (login.php) in which I am asking for username and password.
<form action="processlogin.php" method="post">
<input type="text" name="username">
<input type="password" name="password">
<input type="submit" value="Login">
</form>
Following is the code snippet from my processlogin.php file
if(!$_POST["username"] || !$_POST["password"])
{
$msg = "You left one or more of the required fields.";
echo $msg;
//header("Location:http://localhost/login.php");
}
This code checks whether all the mandatory fields are filled on not. If not, it shows the error message.
Till now everything is fine.
My problem is that, error message is shown in plain white page. I want to show it above the login form in login.php file. How should I change my code to get
my functionality.
I would prefer Jquery Validation or Ajax based Authentication. But still you can do it this way:
Put your Error Message in Session like this :
$_SESSION['Error'] = "You left one or more of the required fields.";
Than simple show it like this:
if( isset($_SESSION['Error']) )
{
echo $_SESSION['Error'];
unset($_SESSION['Error']);
}
In this case you can assign multiple messages in different Operations.
header("Location:http://localhost/login.php?x=1")
In the login.php
if(isset($_GET('x'))){
//your html for error message
}
Hope it helps you,
In processlogin.php,
if(!$_POST["username"] || !$_POST["password"])
{
$msg = "You left one or more of the required fields.";
$msgEncoded = base64_encode($msg);
header("location:login.php?msg=".$msgEncoded);
}
in login.php file,
$msg = base64_decode($_GET['msg']);
if(isset($_GET['msg'])){
if($msg!=""){
echo $msg;
}
}
You can display the message in table or span above the form.
<span>
<?php if(isset($_REQUEST[$msg]))
echo $msg;
?>
</span>
<form>
</form>
And also don't echo $msg in the form's action page.
Try this:
html:
<form action="processlogin.php" method="post">
<input type="text" name="username">
<input type="password" name="password">
<input type="submit" value="Login">
<span>
<?php if(isset($_GET['msg']))
echo $_GET['msg'];
?>
</span>
</form>
php:
if(!$_POST["username"] || !$_POST["password"])
{
$msg = "You left one or more of the required fields.";
header("Location:http://localhost/login.php?msg=$msg");
}
Use only one page (your login.php) to display the form and also to validate its data if sent. So you don't need any $_SESSION variables and you have all in one and the same file which belongs together.
<?php
$msg = null;
if(isset($_GET['send'])) {
if(!$_POST["username"] || !$_POST["password"]){
$msg = "You left one or more of the required fields.";
//header("Location:http://localhost/login.php");
}
}
?>
<?php echo ($msg !== null)?'<p>ERROR: ' . $msg . '</p>':null; ?>
<form action="?send" method="post">
<input type="text" name="username">
<input type="password" name="password">
<input type="submit" value="Login">
</form>
use these functions:
<?php
session_start();
define(FLASH_PREFIX,'Flash_')
function set_flash($key,$val){
$_SESSION[FLASH_PREFIX.$key]=$val;
}
function is_flash($key){
return array_key_exits(FLASH_PREFIX.$key,$_SESSION);
}
function get_flash($key){
return $_SESSION[FLASH_PREFIX.$key];
}
function pop_flash($key){
$ret=$_SESSION[FLASH_PREFIX.$key];
unset($_SESSION[FLASH_PREFIX.$key]);
return $ret;
}
?>
And when you want to send a message to another page use
set_flash('err_msg','one field is empty');
header('location: another.php');
exit();
another.php
<html>
.
.
.
<body>
<?php if(is_flash('err_msg')){?>
<span class="err_msg"><?php echo pop_flash('err_msg'); ?></span>
<?php } ?>
.
.
.
</body></html>
<?php
if($_SERVER['REQUEST_METHOD'] == "POST")
{
if(!$_POST["username"] || !$_POST["password"])
{
$msg = "You left one or more of the required fields.";
echo $msg;
//header("Location:http://localhost/login.php");
}
}
?>
<form action="<?php echo $PHP_SELF;?>" method="post">
<input type="text" name="username">
<input type="password" name="password">
<input type="submit" value="Login">
</form>

Email Validator Problems- Putting it all together

I'm very new to PHP coding.
I've done tons of research to try and help me. As you can imagine I've gotten tons of material for help. The problem is when I'm trying to put it all together.
Specifically here is my problem. I've come across:
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo "e-Mail is Valid";
} else {
echo "Invalid e-Mail";
}
But I have no idea how to implement it. As it stands now the validator checks the fields before the user has time to imput them..... I'm desperate
I'm sure the solution is really simple, but I've spent hours on this and am really desperate for this problem to be solved already.
Here's a link to the page
Here is the code for the page:
<!DOCTYPE html>
<head>
<meta charset='utf-8'>
<title>AWalsh Photography - Contact Me</title>
<link href="style/main_page.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="email_container">
<h1 class="email_head"> Contact Andrew walsh Photography</h1>
<form id="email_form" name="email_form" method="post">
<table>
<tr>
<td><label for="fname">First Name:</label>
</td>
<td><input type="text" name="fname_input" id="fname_input" /><br>
</td>
</tr>
<tr>
<td><label for="lname">Last Name:</label>
</td>
<td><input type="text" name="lname_input" id="lname_input" /><br>
</td>
</tr>
<tr>
<td><label for="email_input">Your Email:</label>
</td>
<td><input type="text" name="email_input" id="email_input" /><br>
</td>
</tr><tr>
<td><label for="email_conf">Re-enter Email:</label>
</td>
<td><input type="text" name="email_conf" id="email_conf" /><br>
</td>
</tr><tr>
<td>
<label for="message_input">Message </label>
</td><td>
<textarea rows="8" cols="45" id="message_input" name="message_input"></textarea>
</td></tr><tr><td></td>
<td>
<input id="submit"type="submit" value="submit" name="submit"/>
</td></tr>
</table>
</form>
<?php
if($_POST['email_imput'] == $_POST['email_conf']){
//stuff to do on success
echo '<h1>Success!!</h1>';
} else {
//stuff to do on failure
echo '<h1>Sorry, The emails you entered do not match</h1>';
}
$email_imput = $_POST['email_imput'];
if (filter_var($email_imput, FILTER_VALIDATE_EMAIL)) {
echo $email_imput . ' is a valid email address.';
} else {
echo $email_imput . ' is not a valid email address.';
}
$message_imput = $_POST['message_imput'];
$msg = "Email address: $email_imput \n" . "Message: $message_imput";
$to = 'myemail#gmail.com ';
$subject = 'AWP_email';
if (filter_var($email_imput)){
mail($to, $subject, $msg, $email);
}
if (mail($to, $subject, $msg, $email)) {
echo("<p>Message successfully sent! Thanks for submitting your message. We will reply to you as soon as possible</p>");
} else {
echo("<h1>Sorry, There was an error in your imput. Please try again.</h1>");
}
?>
<span class="error"><?=$error;?></span>
<form method="post" action="">
<h1> There was an error with your post</h1>
</form>
</div>
</div>
</body>
</html>
Any input would be amazing. Thank you.
You could add a hidden field into the form and check it's value when it's time to send the email.
if (isset($_POST["some_hidden_field"])) {
// put form validation and sending email here
}
else {
// print the form
}
You should first check whether the page has been submitted or not. You might want to try if ($_SERVER['METHOD'] == 'POST') before making any validations

PHP contact form phone validation of the correct amount of numbers

PHP contact form phone validation of the correct amount of numbers
Hello,
I have this php form that validates the content once submitted a sticky php form is what it is called. It keeps the users data in the input box when an error if found so the user dose not have to re-enter all the data again.
When the phone number is submitted I need it to validate that there are 3 characters/numbers in the first input box then 3 in the next then 4 in the last one.
The way it is now as long as you input numbers in the first input box it over looks the rest of the input boxes for the phone number. So I am looking to add a minimum character/number script in the validation process. I have the form validating that it is a number at this time. I also need it to validate that there is the correct amount of numbers in each input box for the phone as well. I believe this is just changing the elseif statements to just if inside another if but that did not work either. Any help would be very appreciated. The Art Institute only taught so much with PHP, and not this.
This is the particular area of the script that validates the phone number:
//validate the phone number
if(is_numeric($_POST['phone01'])) {
$phone = $_POST['phone01']. '-';
}elseif(is_numeric($_POST['phone02'])) {
$phone .= $_POST['phone02']. '-';
}elseif(is_numeric($_POST['phone03'])) {
$phone .= $_POST['phone03'];
}else{
print '<p class="error">Please enter your Phone Number as 10 Number.</p>';
$validate = FALSE;
}
This is a copy of the whole script for the form itself:
<?php
// This page receives the data from itself and validates as well
//error reporting!
ini_set ('display_errors', 1);
//Shows all possible problem!
error_reporting (E_ALL);
// validate email
function isValidEmail($email){
return eregi('^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$', $email);
}
//show form
function show_form($firstName='',$lastName='',$businessName='',$email='',$phone01='',$phone02='',$phone03='',$message=''){
?>
<!--The form starts here -->
<form action ="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="contact form" target="_self" id="contact form" dir="ltr" >
<table bgcolor="#000000" width="525" border="0" align="center">
<tr>
<td width="25%" align="right">*First Name:</td>
<td colspan="2" align="left"><input name="firstName" type="text" id="firstName" tabindex="1" size="30" value="<?php if(isset($_POST['firstName'])) { print htmlspecialchars($_POST['firstName']); }?>"/></td>
</tr>
<tr>
<td align="right">*Last Name:</td>
<td colspan="2" align="left"><input name="lastName" type="text" id="lastName" tabindex="2" size="30" value="<?php if(isset($_POST['lastName'])) {print htmlspecialchars($_POST['lastName']); }?>"/></td>
</tr>
<tr>
<td align="right">Business Name:</td>
<td colspan="2" align="left"><input name="businessName" type="text" id="businessName" tabindex="3" size="35" value="<?php if(isset($_POST['businessName'])) {print htmlspecialchars($_POST['businessName']); }?>"/></td>
</tr>
<tr>
<td align="right">*Email: </td>
<td colspan="2" align="left"><input name="email" type="text" id="email" tabindex="4" size="35" value="<?php if(isset($_POST['email'])) {print htmlspecialchars($_POST['email']); }?>"/></td>
</tr>
<tr>
<td align="right">*Phone Number:</td>
<td colspan="2" align="left">
<input name="phone01" type="text" id="phone01" size="3" maxlength="3" tabindex="5"value="<?php if(isset($_POST['phone01'])) {print htmlspecialchars($_POST['phone01']); }?>"/>
- <input name="phone02" type="text" id="phone02" size="3" maxlength="3" tabindex="6"value="<?php if(isset($_POST['phone02'])) {print htmlspecialchars($_POST['phone02']); }?>"/>
- <input name="phone03" type="text" id="phone03" size="4" maxlength="4" tabindex="7" value="<?php if(isset($_POST['phone03'])) {print htmlspecialchars($_POST['phone03']); }?>"/></td>
</tr>
<tr align="center">
<td align="right">*Message:</td>
<td colspan="2" align="left"><textarea name="message" type="text" id="message" tabindex="8" cols="45" rows="4"><?php if(isset($_POST['message'])) {print htmlspecialchars($_POST['message']); }?></textarea>
</td>
</tr>
<tr align="center">
<td> </td>
<td><input name="submit" type="submit" tabindex="9" value="Email" /></td>
<td><input type="reset" name="reset" id="reset" value=" Reset " tabindex="10"/></td>
</tr>
</table>
</form>
<?php
} // end of show_form function
$validate = TRUE;
if($_SERVER['REQUEST_METHOD']!='POST') {
show_form();
} else {
//validate form fields
//validate the first name
if(empty($_POST['firstName'])) {
print '<p class="error">Please enter your First Name.</p>';
$validate = FALSE;
}
//validate the last name
if(empty($_POST['lastName'])) {
print '<p class="error">Please enter your Last Name.</p>';
$validate = FALSE;
}
//validate the enail with email arrary
if(!isValidEmail($_POST['email'])) {
print '<p class="error">Please enter your Email Address in the correct formate.</p>';
$validate = FALSE;
}
//validate the phone number
if(is_numeric($_POST['phone01'])) {
$phone = $_POST['phone01']. '-';
}elseif(is_numeric($_POST['phone02'])) {
$phone .= $_POST['phone02']. '-';
}elseif(is_numeric($_POST['phone03'])) {
$phone .= $_POST['phone03'];
}else{
print '<p class="error">Please enter your Phone Number as 10 Number.</p>';
$validate = FALSE;
}
//validate the message
if(empty($_POST['message'])) {
print '<p class="error">Please enter your Messagee.</p>';
$validate = FALSE;
}
if(!$validate){
print "<p>Please fill in all the fields with an asterisk * next to it and than please try again!</p>";
show_form($_POST['firstName'],$_POST['lastName'],$_POST['businessName'],$_POST['email'],$_POST['phone01'],$_POST['phone02'],$_POST['phone03'],$_POST['message']);
}else{
$phone01 = $_POST['phone01'];
$phone02 = $_POST['phone02'];
$phone03 = $_POST['phone03'];
$phone = $phone01.'-'.$phone02.'-'.$phone03;
//confirmation email to client includes all information provided
mail($_POST['email'], 'Contact Confirmation from www.Ozbar.net Web site', 'Thank You '.$_POST['firstName'].' '.$_POST['lastName'].' for your request for us to contact you.
Below is the information your provided us to contact you per your request.
First Name: '.$_POST['firstName'].'
Last Name: '.$_POST['lastName'].'
Business Name: '.$_POST['businessName'].'
Email Address: '.$_POST['email'].'
Phone Number: '.$_POST['phone01'].'-'.$_POST['phone02'].'-'.$_POST['phone01'].'
Message: '.$_POST['message'].' ','From:contact#steveoatman.me);
//notice of a new contact request
mail('contact#steveoatman.me, 'Contact Request from www.Steveoatman.me Web site', '
First Name: '.$_POST['firstName'].'
Last Word: '.$_POST['lastName'].'
Business Name: '.$_POST['businessName'].'
Email Address: '.$_POST['email'].'
Phone Number: '.$_POST['phone01'].'-'.$_POST['phone02'].'-'.$_POST['phone01'].'
Message: '.$_POST['message'].' ','From:contact#steveoatman.me);
print '<p align="center">Thank You For Your Request!</p>'?><br /><?php
print '<p align="center">We will contact you back with in 24-48 hours.</p>'
?>
<br /><br /> <!-- if all validated a thank you statement -->
<?php
}
} //end of IF submit
// end of all php
?>
<!-- end of #ref form -->
Use strlen to validate the field lengths. Do not use if/elseif as you want to verify all three inputs. Set a flag to keep track of the validity of the phone number.
$invalid_phone = false;
if((strlen($_POST['phone01']) == 3) && is_numeric($_POST['phone01'])) {
$phone = $_POST['phone01']. '-';
}else{
$invalid_phone = true;
}
if((strlen($_POST['phone02']) == 3) && is_numeric($_POST['phone02'])) {
$phone .= $_POST['phone02']. '-';
}else{
$invalid_phone = true;
}
if((strlen($_POST['phone03']) == 4) && is_numeric($_POST['phone03'])) {
$phone .= $_POST['phone03'];
}else{
$invalid_phone = true;
}
if($invalid_phone){
print '<p class="error">Please enter your Phone Number as 10 Number.</p>';
$validate = FALSE;
}
The code above is just checking whether any of the 3 fields have a number in them, rather than all of them.
To achieve what you are going for above, something like this would do it:
if (is_numeric($_POST['phone01']) && is_numeric($_POST['phone02']) && is_numeric($_POST['phone03']))
{
$phone = $_POST['phone01']."-".$_POST['phone02']."-".$_POST['phone03'];
}
else
{
print '<p class="error">Please enter your Phone Number as 10 Number.</p>';
$validate = FALSE;
}
However, the above code does not do any other kind of validation, such as checking to see that the required number of digits have been put in each form field.
You might also want to use the 'ctype_digit()' function to make sure that only digits are entered, rather than a numric string such as 1.3.
So you could do something like
if (!ctype_digit($_POST['phone01']) || strlen($_POST['phone01']) != 4)
{
$validate = FALSE;
}

Categories