PHP Form Error Message on Form Page - php

I am pretty new to PHP and I can't seem to find what I am looking for.
I want the error to show on the page where the form is filled out.
I want a "Please enter name" error to show if it's blank in the $showerror part of the form page.
This is what I have so far...
<form method="post" action="process.php">
<table width="667">
<td colspan="2"><?php echo $showerror; ?></td>
<tr>
<td>Full Name:*</td>
<td><input class="text" name="name" placeholder="Ex. John Smith"></td>
<tr>
<td>E-mail:*</td>
<td><input class="text" name="email" placeholder="Ex. johnsmith#gmail.com"></td>
<tr>
<td>Type of site:*</td>
<td>Business<input name="multioption[]" type="radio" value="Business" class="check" /> Portfolio<input name="multioption[]" type="radio" value="Portfolio" class="check" /> Forum<input name="multioption[]" type="radio" value="Forum" class="check" /> Blog<input name="multioption[]" type="radio" value="Blog" class="check" /></td>
<tr>
<td>Anti-Spam: (2)+2=?*</td>
<td><input name="human" placeholder="What is it?"></td>
</table>
<input id="submit" name="submit" type="submit" value="Submit"></form>
Then this is what my process.php looks like... I am not sure how to code the error part.
<?php
$name = isset($_POST['name']) ? $_POST['name'] : '';
$email = isset($_POST['email']) ? $_POST['email'] : '';
$human = isset($_POST['human']) ? $_POST['human'] : '';
$submit = isset($_POST['submit']) ? true : false;
$multioption = isset($_POST['multioption'])
? implode(', ', $_POST['multioption'])
: 'No multioption option selected.';
$from = 'From: Testing Form';
$to = 'xx#xxxxx.com';
$subject = 'Testing Form';
$body =
"Name: $name\n
E-Mail: $email\n
Multi Options: $multioption\n";
if ($submit && $human == '4') {
mail ($to, $subject, $body, $from);
print ("Thank you. We have received your inquiry.");
}
else {
echo "We have detected you are a robot!.";
}
?>

You need to place PHP syntax between PHP tags, for example, this line:
<td colspan="2">$showerror</td>
becomes like this:
<td colspan="2"><?php echo $showerror; ?></td>
If you're completely new to PHP, you can start learning from some tutorial websites, here's a good one
EDIT:
You can set $showerror in the PHP page, this could be a long list of "if conditions" for each form field, but I will show you a small/simple example for the full-name $_POST['name'], it will be like this:
$showerror = '';
if(!empty($_POST['name'])) {// enter here if fullname is set
if(strlen($_POST['name']) >= 6 && strlen($_POST['name']) <= 12) {// enter here if fullname length is between 6-12 characters
// You can do more validation by using "if conditions" here if you would like, or keep it empty if you think fullname is correct
} else {// enter here if fullname is NOT between 6-12 characters
$showerror = 'Full name must be 6-12 characters';
}
} else {// enter here if fullname is not set
$showerror = 'Please enter your full name';
}

compiler only parse content as php which is written between <?php ?> tag so use
<td colspan="2"><?php echo $showerror; ?></td>
or
<td colspan="2"><?= $showerror ?></td>

Try this,
<?php
if(isset($showerror))
echo $showerror;
else
echo '';
?>
You will get an error message Undefined variable if you do not have a code in validating the $showerror variable on the first compilation of the page

Try this.. you have to put both the code in same file.
and can check if the request is post the only read the php.
and then you can put the value in $sho

Related

PHP Forms Trouble with the elusive $_POST

Hey Guys this should be probably really simple I am just missing a step.
So I have a form and I want to make sure all values are added before moving to the next page that processes my values and sends them to my email. I also have error messages if someone does not add a value. The error messages display on the reloading of the page.
The problem I have is that you have to reload the page for the Superglobal Post to contain any values. For some reason it does not add them until the page is reloaded. So what happens is if you fill all 5 input fields you have to reload the page, and then submit again for it to send it to my send_form_email.php script because at that point the values are added. I want it to logically reload if any of the values are empty (which will display error messages telling the user that the input field must have content), and automatically send the user to send_form_email.php if all values have been correctly added.
It's almost pull my hair out so if someone could help me understand what piece of the puzzle I am missing I would be so grateful!
<form name="contactform" method="post" action="<?php echo $value; ?>">
<table width="450px">
<tr>
<td valign="top">
</td>
<td valign="top">
<input class="inputs" placeholder="firstname" type="text" name="first_name" maxlength="50" size="30" placeholder="name"><br>
<span class="error">* <?php echo $firstErr;?></span>
</td>
</tr>
<tr>
<td valign="top">
</td>
<td valign="top">
<input class="inputs" placeholder="lastname" type="text" name="last_name" maxlength="50" size="30">
<span class="error">* <?php echo $lastErr;?></span>
</td>
</tr>
<tr>
<td valign="top">
</td>
<td valign="top">
<input class="inputs" placeholder="email" type="text" name="email" maxlength="80" size="30">
<span class="error">* <?php echo $emailErr;?></span>
</td>
</tr>
<tr>
<td valign="top">
</td>
<td valign="top">
<input class="inputs" placeholder="telephone" type="text" name="telephone" maxlength="30" size="30">
<span class="error">* <?php echo $telephoneErr;?></span>
</td>
</tr>
<tr>
<td valign="top">
</td>
<td valign="top">
<textarea class="inputs" placeholder="comments" name="comments" maxlength="1000" cols="25" rows="6"></textarea>
<span class="error">* <?php echo $commentsErr;?></span>
</td>
</tr>
<tr>
<td colspan="2" style="text-align:center">
<input type="submit" value="Submit" style="background-color: #0F6D87;
font-family: Exo-Light;
color: #000000;
width: 75px;
font-weight: bold;
border-color: #003D69;
border-style: outset;
font-size: .8em;
box-shadow: 2px 2px 2px rgba(0, 34, 97, 0.6);">
<INPUT TYPE="RESET">
</td>
</tr>
</table>
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST"){
if (!empty($_POST["first_name"]) && !empty($_POST["last_name"]) && !empty($_POST["email"]) && !empty($_POST["telephone"]) && !empty($_POST["comments"])) {
$value ="http://cdubach.com/inc/send_form_email.php";
} elseif (empty($_POST["first_name"]) || empty($_POST["last_name"]) || empty($_POST["email"]) || empty($_POST["telephone"]) || empty($_POST["comments"])){
$value = "#";
}
}
$first_name = $_POST['first_name']; // required
$last_name = $_POST['last_name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['telephone']; // not required
$comments = $_POST['comments']; // required
echo $_POST['submit'] . " = Submit <br>";
echo $_POST["first_name"] . " = First Name <br>";
echo $_POST["last_name"] . " = Last Name <br>";
echo $_POST["email"] . "= Email <br>";
echo $_POST["telephone"] . "= Telephone <br>";
echo $_POST["comments"] . "= Comments <br>";
echo var_dump($_Post) . "= Dump <br>";
echo $value . " = Value <br>" ;
echo $_SERVER["PHP_SELF"];
header('Content-Type: text/plain');
var_dump(htmlspecialchars("<a href='test'>Test</a>", ENT_QUOTES, 'UTF-8'));
echo "<br>";
echo htmlspecialchars("<a href='test'>Test</a>", ENT_XHTML, 'UTF-8');
echo "<br>";
$str = "A 'quote' is <b>bold</b>";
/* */
//convert from utf8
$str = utf8_decode($str);
//translate HMTL entities
$trans = get_html_translation_table(HTML_ENTITIES);
$str = strtr($str, $trans);
echo htmlspecialchars($str);
echo "<br>";
echo htmlentities($str, ENT_QUOTES);
$new = htmlspecialchars("<a href='test'>Test</a>", ENT_QUOTES);
echo $new; // <a href='test'>Test</a>
if ($_SERVER["REQUEST_METHOD"] == "POST") {
//Check First Name Field if Nothing Post Error
if (empty($_POST["first_name"])) {
$firstErr = "Name is required";
} else {
$firstErr = test_input($_POST["name"]);
}
//Check Last Name Field if Nothing Post Error
if (empty($_POST["last_name"])) {
$lastErr = "Last Name is required";
} else {
$lastErr = test_input($_POST["last_name"]);
}
//Check Email Field if Nothing Post Error
if (empty($_POST["email"])) {
$emailErr = "Email is required";
} else {
$emailErr = test_input($_POST["email"]);
}
//Check Telephone Field if Nothing Post Error
if (empty($_POST["telephone"])) {
$telephoneErr = "Telephone is Required";
} else {
$telephoneErr = test_input($_POST["telephone"]);
}
//Check Comments Field if Nothing Post Error
if (empty($_POST["comments"])) {
$commentsErr = "Comments is Required";
} else {
$commentsErr = test_input($_POST["comments"]);
}
//Check Comments
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
The reason you need to reload a second time in order for the script to work is because the action property of the form is not populated on the first run (since $value is not set), on the 2nd run however it is (if the $_POST pass the checks you have set) and is set to http://cdubach.com/inc/send_form_email.php.
You will see this if you check the actual html code in the first and second run.
However this is only one of the problems with your script. Some hints:
remove the header('Content-Type: text/plain');, that line instructs the browser to treat the page as text and it will not render it as html.
move the whole html after your PHP script - that way the errors messages you have prepared will work as they should
finally to resolve your problem with "$_POST only if all ok" condition check the validity of the script with client side JavaScript. On another hand if you have access over the called script (the one that $value points to) you could make the check there and redirect to the form if you should.

PHP Code Error. Can't find my logic mistake

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.';
}

if statement's not working

Forgive my noobness coding as I teach myself PHP (lol)!
The code below works almost perfectly for a form I want to create. Whenever the user hits the submit button, provided the form is all filled in correctly, the text "E-mail Sent Successfully" message appears at the top of the page. If any of the mandatory fields are incomplete or invalid, then an error message is returned underneath the corresponding input field and the "e-mail sent successfully" message does not appear. This works properly for all but the last if statement (antispam). Essentially, if you enter the correct answer (3) into the antispam box, then regardless of the other fields being filled in or not, the "e-mail Sent Successfully" message appears (it shouldn't do this as other fields are not filled out correctly). The "e-mail Sent Successfully" message should only appear if all the mandatory fields are completed correctly.
Whatever 'if statement' goes at the bottom adhere's to the same rule (i.e. if name was at the bottom of the if statements, then only name would need to be entered correctly for the e-mail sent successfully message to appear). It feels as though if the last if statement is passed as true, the other statements don't work.
<?php
if (isset($_POST['submit'])) {
$yourname = $_POST['yourname'];
$organisation = $_POST['organisation'];
$email_from = $_POST['email'];
$comments = $_POST['comments'];
$antispam = $_POST['antispam'];
$namestring = "/^[A-Za-z .'-]+$/";
$emailstring = '/^[A-Za-z0-9._%-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
$error_message_email = "";
$error_message_name = "";
$error_message_message = "";
$error_message_antispam = "";
if(!preg_match($namestring,$yourname)) {
$error_message_name .= "Please enter a valid name";
}
if(!preg_match($emailstring,$email_from)) {
$error_message_email .= "Please enter a valid e-mail address";
}
if(strlen($comments) < 4) {
$error_message_message .= "Please enter a valid message";
}
if($antispam != "3") {
$error_message_antispam .= "Please enter a valid answer";
}
else {
echo "E-mail Sent Successfully";
}
}
else {
$yourname="";
$organisation="";
$email_from="";
$comments="";
$antispam="";
$error_message_email = "";
$error_message_name = "";
$error_message_message = "";
$error_message_antispam = "";
}
?>
<form name="htmlform" method="post" action="ifandelse.php">
<table width="650px">
<tr>
<td>
<label for="yourname"><font color="#ff0000">*</font>Name:</label>
</td>
<td>
<input type="text" name="yourname" value="<?PHP echo$yourname;?>" maxlength="50" size="40"><br>
<div class="warning">
<?php print $error_message_name;?>
</div>
</td>
</tr>
<tr>
<td>
<label for="organisation">Organisation:</label>
</td>
<td>
<input type="text" name="organisation" value="<?PHP echo$organisation;?>"maxlength="50" size="40">
</td>
</tr>
<tr>
<td>
<label for="email"><font color="#ff0000">*</font>E-mail Address:</label>
</td>
<td>
<input type="text" name="email" value="<?PHP echo$email_from;?>" maxlength="80" size="40"><br>
<div class="warning">
<?php print $error_message_email;?>
</div>
</td>
</tr>
<tr>
<td valign="top">
<label for="comments"><font color="#ff0000">*</font>Message:<br></label>
</td>
<td>
<textarea name="comments" rows="8" cols="60"><?PHP echo$comments;?></textarea><br>
<div class="warning">
<?php print $error_message_message;?>
</div>
</td>
</tr>
<tr>
<td colspan="2">
<font color="#ff0000">*</font>To avoid unwanted spam, please answer the following question correctly: 2 + 1 = <input type="text" name="antispam" value="<?PHP echo$antispam?>" maxlength="100" style="width:30px"><br>
<div class="warning">
<?php print $error_message_antispam;?>
</div>
</td>
</tr>
<tr>
<td>
</td>
<td style="text-align:center;">
<input type="submit" class="formsubmit" name="submit" value="Submit Form">
</td>
</tr>
</table>
</form>
Be most grateful for any help, I've had several friends look into this and we still can't figure out why it's doing this. Have tried nesting the 'if statements' differently, changing the if statement's to else if, etc with no joy :(
I've looked on the forums and can't find a solution that works for me.
You should use a variable to know if there was an error (or more than one) at the end of your verifications. Some code below to give you the general idea :
$isError = false;
if (!preg_match($namestring, $yourname)) {
$error_message_name .= "Please enter a valid name";
$isError = true;
}
if (!preg_match($emailstring, $email_from)) {
$error_message_email .= "Please enter a valid e-mail address";
$isError = true;
}
if (strlen($comments) < 4) {
$error_message_message .= "Please enter a valid message";
$isError = true;
}
if ($antispam != "3") {
$error_message_antispam .= "Please enter a valid answer";
$isError = true;
}
if ($isError) {
//do wathever you have to do
} else {
//send mail
echo "E-mail Sent Successfully";
}
The else statement is attached to you last if and only to this one. Hence, only the last condition matters regarding whether your success message is displayed or not. You'll have to chain your checks :
if (!preg_match($namestring, $yourname)) {
$error_message_name .= "Please enter a valid name";
}
elseif (!preg_match($emailstring, $email_from)) {
$error_message_email .= "Please enter a valid e-mail address";
}
elseif (strlen($comments) < 4) {
$error_message_message .= "Please enter a valid message";
}
elseif ($antispam != "3") {
$error_message_antispam .= "Please enter a valid answer";
} else {
echo "E-mail Sent Successfully";
}
Of course, this will still leave you displaying only one error message at a time, which is far from optimized but that's another matter.

Undefined Index?

I'm working on a very simple, very easy contact form and when i did it on a separate page it worked perfectly, but when i added it to the current website it can't get the $_POST i don't know why. here are the codes
$to ="enter email here";
$name = $_POST["name"];
$email = $_POST["email"];
$header = "From " . $name;
$message = $_POST["message"];
$content = "From: ". $name ."<br /> Email: " . $email ."<br /> Message: " . $message;
if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
echo "illegal email";
}
else
{
if (!empty($name) && !empty($message)){
mail($to, $header, $content);
echo"sent <br />";
echo $content;
}else
{
if(empty($email))
{
echo "your email is empty";
}
elseif(empty($name))
{
echo "please enter your name";
}
elseif(empty($message)){
echo "can't send empty messages";
}
}
}
html
<form method="post" action="mail.php">
<table>
<tr>
<td>
Name:
</td>
<td>
<input type="text" name="name" />
</td>
</tr>
<tr>
<td>
Email:
</td>
<td>
<input type="text" name="email"/>
</td>
</tr>
<tr>
<td>
Subject:
</td>
<td>
<input type="text" name="subject"/>
</td>
</tr>
<tr>
<td>
Message: <br /><br/><br/>
</td>
<td>
<textarea style="resize:vertical;" name="message"></textarea>
</td>
</tr>
<tr>
<td>
</td>
<td>
<input type="submit"/>
</td>
</tr>
</table>
</form>
thanks in advance and sorry if its a repeat
Check that the PHP is actually executing by adding something like this to the top:
echo "Testing PHP...";
If you do not see that output after submitting a form, check that you are posting the form to the right file. For example, you might need to use:
<form method="post" action="/mail.php">
or
<form method="post" action="/php/mail.php">
...code depending on your website structure.
It's certainly not $_POST that's broken, so it must be something either server related or an error in your code.
Do you have any other PHP on the website your importing the form to? If so you need to make sure that it isn't affecting it in any way.
One more thing to check, it has been reported that a PHP update accidently changed the upload limit size from "8M" to "10MB". Have a scan through your php.ini file and make sure that their isn't any unwanted "MB" instead of "M" in your upload limit.
One final suggestion I can give if you still haven't found the cause after this, is try using:
<?php var_dump($_POST); ?>
which should reveal what's really there.

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