Hello all! My first post…here it goes!
Im trying to build upon the “PHP Contact Form” tutorial. I’m fairly new to PHP, this would be my first hands on task.
Using the code (minus checkboxes and radio buttons) as is…I am trying to accomplish three things:
- Add some “field validator” code so that my visitor gets a prompt or error message telling them that “email” and “phone” fields are a must otherwise the form does not submit until complete. Message preferably on the same page as the form.
- Right now, when i receive the inquiries…the “From” field says “From:Nobody”. I’d like to correct that. Right now it only shows the persons name in the body of the email.
- Lastly not as critical as 1 & 2…is there a way to reply directly to the person from the designated email address based on the persons email address as inputed on the form?
Any help is much appreciated! Thanks!
Here is the link to the form (not pretty yet) : http://www.think-epic.com/contact.html
Here is my PHP:
<?php
if(isset($_POST[‘submit’])) {
$to = "mikemera@think-epic.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'];
$body = "From: $name_field
E-Mail: $email_field
Company: $company_field
Phone: $phone_field
Address: $address_field
Message:
$message
";
header( "Location: http://www.think-epic.com/thankyou.html" );
mail($to, $subject, $body);
} else {
echo “blarg!”;
}
?>
-
if(!$_POST['name']) echo 'name is required';
2.3.
$headers = "From: $name_field <$email_field>
";
$headers .= "Reply-To: $name_field <$email_field>
";
mail($to, $subject, $body, **[COLOR="Blue"]$headers[/COLOR]**);
Thanks Kitsunegari for the help! Gonna go try this right now.
[quote=kitsunegari;2324644]1.
if(!$_POST['name']) echo 'name is required';
2.3.
$headers = "From: $name_field <$email_field>
";
$headers .= "Reply-To: $name_field <$email_field>
";
mail($to, $subject, $body, **[COLOR=Blue]$headers[/COLOR]**);
[/quote]
Kitsunegari, I have the “From” and “Reply to” portion working beautifully. I cant seem to get the validation to work? I may have placed your snippet of code in the wrong place - the form still goes thru even is the “Name” field is empty. The echo should appear within the same page? Help?
Here’s my revised PHP:
<?php
if(isset($_POST[‘submit’]))
if(!$_POST[‘name’])
{
$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.think-epic.com/thankyou.html" );
mail($to, $subject, $body, $headers);
} else {
echo “name is required”;
}
?>
Thanks for your attention thus far! Further guidance would be appreciated.
You must invert your if/else blocks
if(!$_POST[‘name’]) means: if name is empty, don’t send the email
whereas what you did in your code means: if name is empty, send the email
basically, put what is in your else block in the if block and vice versa
Thanks Kitsunegari for your help. Maybe I should have been more clear…I am 100% novice when it comes to PHP. I obviously misunderstood what you meant in your last reply. This is what I interpreted and obviously its not working for me:
<?php
if(isset($_POST[‘submit’]))
{
$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.think-epic.com/thankyou.html" );
mail($to, $subject, $body, $headers);
}
elseif (!$_POST[‘name’]) echo ‘name is required’;
}
?>
I dont know rules and proper formatting other than tweaking exisitng code. Thanks.
The function isset check if the $_POST variable “submit” exists in your form. As you always click on your submit button to send your form, the condition always returns true, that’s why it’s always sent.
<?php
if($_POST['submit'] && $_POST['name']) // add the others conditions you need here
{
$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.think-epic.com/thankyou.html" );
mail($to, $subject, $body, $headers);
}
elseif (!$_POST['name']) echo 'name is required';
}
?>
As kitsunegari said, isset($_POST[‘name’]) only checks to see if the KEY exists, a better way might be:
if($_POST['name'] != '') {
if($_POST['phone'] != ''){ //You could use preg_match to check for a valid phone number
$to = "mike_mera@hotmail.com";
//REST OF CODE HERE
}
else {
echo "Please Enter Phone Number";
}
}
else {
echo "Please Enter Name";
}
If you only want to check that the user entered something, I would just use javascript validation. For example, just change your opening form tag to:
*
*
and then, in the head of the page or preferably in a external .js file, make a function like so:
*
function validate()
{
var thisForm=document.contactForm;
var error = '';
if(thisForm.name.value == '') {error='Please enter your name!
';}
if(thisForm.phone.value == '') {error='Please enter your phone number!
';}
if(error != '') {
alert(error);
return false;
}
else {
return true;
}
}*
Thanks to both of you for your help and patience. It’s becoming a bit clearer now. Big help!