need help with placing IF ELSE on contact form

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bruceg
Forum Contributor
Posts: 174
Joined: Wed Mar 16, 2005 11:07 am
Location: Morrisville, NC
Contact:

need help with placing IF ELSE on contact form

Post by bruceg »

with my form validation code

Code: Select all

<?php
 
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$company = $_POST['company'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$email2 = $_POST['email2'];
$URL = $_POST['URL'];
$Contact_Preference = $_POST['Contact_Preference'];
$Contact_Time = $_POST['Contact_Time'];
$message = $_POST['Message'];
 
if ((!$firstname) || (!$Contact_Preference)) {
 
    echo'<p><strong>Error!</strong> Fields marked <span class="red"> *</span> are required to continue.</p><br />';
echo'<p>Please go back to the <a href="Contact.php"title="Contact Me">Contact Me</a> page and try it again!</p>';
} 
    
if (!eregi("^[a-z0-9]+([_\\.-][a-z0-9]+)*"."@"."([a-z0-9]+([\.-][a-z0-9]+)*)+"."\\.[a-z]{2,}"."$",$email)) {
 
    echo '<p>Invalid email address entered.</p><br />';
echo '<p>Please go back to the <a href="Contact.php" title="Contact Me">Contact Me</a> page and try it again!</p>';
}
if (($email) != ($email2)) {
 
    echo '<strong>Error!</strong> e-mail addresses dont match.<br />';

 
}
 
$email_address = "webguync@gmail.com";
$subject = "There has been a disturbance in the force";
 
$message = "Request from: $firstname $lastname\n\n
Company name: $company\n
Phone Number:  $phone\n
Email Address: $email\n
URL: $URL\n
Please Contact me via: $Contact_Preference\n
The best time to reach me is: $Contact_Time\n
I wish to request the following additional information: $Textarea";
 
mail($email_address, $subject, $message, "From: $email \nX-Mailer: PHP/" . phpversion());

 
echo "<p>Hello, <strong>$firstname</strong>.<br /><br />
We have received your request for additional information, and will respond shortly.<br />
Thanks for visiting inspired-evolution.com and have a wonderful day!<br /><br />
Regards,<br /><br />
<strong>Inspired Evolution</strong></p>";

?>
I need to add...

if(this error || that error || some other error) {
    print error;
} else {
    //no errors, thank them!
   print THANKS!
}

to get it to work like I want, but I am not sure where to add the || and else in the above code.

any help??

thanks in advance!
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post by ol4pr0 »

Code: Select all

if (isset($_POST['submit'])) {
if(strlen($_POST['whatever'])>0){
$check_1 ="0";
} else { 
$check_1 = "1"; 
}
if(strlen($_POST['whatever'])>0){
$check_2 ="0";
} else { 
$check_2 = "1"; 
}
if ($check_1==1 && $check_2==1) {
//do mail
} else {
// show form again 
} 
}# end post submit
# <form ect....
bruceg
Forum Contributor
Posts: 174
Joined: Wed Mar 16, 2005 11:07 am
Location: Morrisville, NC
Contact:

Post by bruceg »

can you explain this part:

Code: Select all

if(strlen($_POST['whatever'])>0){

especially what I need to replace with whatever...

thanks,
neugent
Forum Newbie
Posts: 24
Joined: Wed Jul 06, 2005 3:35 am
Location: Philippines

Post by neugent »

it counts how many strings are there in your $_POST['whatever'], if its greater than 1 then go continue, else show error, return to form and then complete entries. :D
bruceg
Forum Contributor
Posts: 174
Joined: Wed Mar 16, 2005 11:07 am
Location: Morrisville, NC
Contact:

Post by bruceg »

couple of things...

does the form need to have this to display errors on the same page

<form method="post" action="\$_SERVER['PHP_SELF]\" class="info_request">

and this is going to be wrong, so someone please tell me what I need to fix.

Code: Select all

<?php
 
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$company = $_POST['company'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$email2 = $_POST['email2'];
$URL = $_POST['URL'];
$Contact_Preference = $_POST['Contact_Preference'];
$Contact_Time = $_POST['Contact_Time'];
$message = $_POST['Message'];

if (isset($_POST['submit'])) {
if(strlen($_POST['does the thanks or error msg go here?'])>0){
$check_1 ="0";
} else { 
$check_1 = "1"; 
}
if(strlen($_POST['does the thanks or error msg go here?'])>0){
$check_2 ="0";
} else { 
$check_2 = "1"; 
}
if ($check_1==1 && $check_2==1) {
//do mail
} else {
// show form again 
} 
}# end post submit

 
if ((!$firstname) || (!$Contact_Preference)) {
 
    echo'<p><strong>Error!</strong> Fields marked <span class="red"> *</span> are required for the submission to work.</p><br />';

} 
    
if (!eregi("^[a-z0-9]+([_\\.-][a-z0-9]+)*"."@"."([a-z0-9]+([\.-][a-z0-9]+)*)+"."\\.[a-z]{2,}"."$",$email)) {
 
    echo '<p>Invalid email address entered.</p><br />';
echo '<p>Please try it again!</p>';
}
if (($email) != ($email2)) {
 
    echo '<strong>Error!</strong> e-mail addresses don't match.<br />';

 
}
 
$email_address = "webguync@gmail.com";
$subject = "There has been a disturbance in the force";
 
$message = "Request from: $firstname $lastname\n\n
Company name: $company\n
Phone Number:  $phone\n
Email Address: $email\n
URL: $URL\n
Please Contact me via: $Contact_Preference\n
The best time to reach me is: $Contact_Time\n
I wish to request the following additional information: $Textarea";
 
mail($email_address, $subject, $message, "From: $email \nX-Mailer: PHP/" . phpversion());

 
echo "<p>Hello, <strong>$firstname</strong>.<br /><br />
We have received your request for additional information, and will respond shortly.<br />
Thanks for visiting inspired-evolution.com and have a wonderful day!<br /><br />
Regards,<br /><br />
<strong>Inspired Evolution</strong></p>";

?>
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post by ol4pr0 »

Code: Select all

<?
if (isset($_POST['submit'])) {
if(strlen($_POST['name'])>0){
$check_1 = "ERROR MSG";
} else { 
$check_1 = "1"; 
}
if(strlen($_POST['street'])>0){
$check_2 ="ERROR MSG";
} else { 
$check_2 = "1"; 
}
# then something like this
if ($check_1 !='1') {?>
<input type="text" name="street" value="" /><?php echo $check_1;?>
<?} else {?>
<input type="text" name="street" value="" />
<?
}
Post Reply