Hello. I have this contact form that I tweaked a few weeks back with the help of some fellow Kirupa member. Its worked fine thus far. I need to know how to insert additional “Conditions”. I am completely in the dark with PHP. Below is what “I thought” would work going on a hunch. Apparently NOT. Can someone please help me write the correct code for it? My hunch was close but wrong nevertheless.
Im sure some of you out there are laughing at how simple this could be fixed being that you are experts. Thanks in advance!
<?php
if($_POST[‘submit’] && $_POST[‘name’])
if($_POST[‘submit’] && $_POST[‘email’])
{
$to = "mike_mera@hotmail.com ";
$subject = “Contact Form”;
$name_field = $_POST[‘name’];
$email_field = $_POST[‘email’];
$company_field = $_POST[‘company’];
$phone_field = $_POST[‘phone’];
$address_field = $_POST[‘address’];
$message = $_POST[‘message’];
$headers = "From: $name_field <$email_field>
";
$headers .= "Reply-To: $name_field <$email_field>
";
$body = "From: $name_field
E-Mail: $email_field
Company: $company_field
Phone: $phone_field
Address: $address_field
Message:
$message
";
header( “Location: http://www.mgmtransformer/thankyou.html ” );
mail($to, $subject, $body, $headers);
}
elseif (!$_POST[‘name’]) echo ‘name is required’;
elseif (!$_POST[‘email’]) echo ‘email is required’;
?>
sekasi
June 6, 2008, 11:29pm
2
<?php
if($_POST['submit'] && $_POST['name']) && $_POST['email']) {
$to = "mike_mera@hotmail.com";
$subject = "Contact Form";
$name_field = $_POST['name'];
$email_field = $_POST['email'];
$company_field = $_POST['company'];
$phone_field = $_POST['phone'];
$address_field = $_POST['address'];
$message = $_POST['message'];
$headers = "From: $name_field <$email_field>
";
$headers .= "Reply-To: $name_field <$email_field>
";
$body = "From: $name_field
E-Mail: $email_field
Company: $company_field
Phone: $phone_field
Address: $address_field
Message:
$message
";
header( "Location: http://www.mgmtransformer/thankyou.html" );
mail($to, $subject, $body, $headers);
}
else {
if (!$_POST['name']) echo 'name is required';
elseif (!$_POST['email']) echo 'email is required';
}
?>
sekasi
June 6, 2008, 11:30pm
3
edit, might want to put in a else { } after that last elseif, just for code validation…
djheru
June 7, 2008, 12:44am
4
In the if conditional, I like to use
if(isset($_POST['submit']))
{
if($_POST['name'] != '')
{
if($_POST['email'] != '')
{
//Strip out newline character and send email
}
else
{
echo "You forgot email";
}
}
else
{
echo "You forgot name";
}
}
If you submit a form with blank text input fields, PHP still creates a $_POST value, it is just an empty string. So if you test for
if($_POST['name'])
It will still evaluate to true.