# PHP contact form - Please Help w/minor tweaks

**URL:** <https://forum.kirupa.com/t/php-contact-form-please-help-w-minor-tweaks/259986>\
**Category:** programming\
**Created:** [May 11, 2008, 11:09am UTC](https://forum.kirupa.com/t/php-contact-form-please-help-w-minor-tweaks/259986 "2008-05-11T11:09:58Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![mikeepic](https://avatars.discourse-cdn.com/v4/letter/m/2bfe46/32.png) [@mikeepic](https://forum.kirupa.com/u/mikeepic)\
**Post date:** [May 11, 2008, 11:09am UTC](https://forum.kirupa.com/t/php-contact-form-please-help-w-minor-tweaks/259986/1 "2008-05-11T11:09:58Z")

</div>

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:

1. 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.
2. 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.
3. 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](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!”;  
}  
?\>

---

<div class="post-metadata">

**Author:** ![kitsunegari](https://avatars.discourse-cdn.com/v4/letter/k/eb8c5e/32.png) [@kitsunegari](https://forum.kirupa.com/u/kitsunegari)\
**Post date:** [May 11, 2008, 1:57pm UTC](https://forum.kirupa.com/t/php-contact-form-please-help-w-minor-tweaks/259986/2 "2008-05-11T13:57:06Z")

</div>

1. 

```auto

if(!$_POST['name']) echo 'name is required';

```

2.3.

```auto

$headers = "From: $name_field <$email_field>
";
$headers .= "Reply-To: $name_field <$email_field>
";

mail($to, $subject, $body, **[COLOR="Blue"]$headers[/COLOR]**);

```

---

<div class="post-metadata">

**Author:** ![mikeepic](https://avatars.discourse-cdn.com/v4/letter/m/2bfe46/32.png) [@mikeepic](https://forum.kirupa.com/u/mikeepic)\
**Post date:** [May 11, 2008, 5:42pm UTC](https://forum.kirupa.com/t/php-contact-form-please-help-w-minor-tweaks/259986/3 "2008-05-11T17:42:13Z")

</div>

Thanks Kitsunegari for the help! Gonna go try this right now.

[quote=kitsunegari;2324644]1.

```auto

if(!$_POST['name']) echo 'name is required';

```

2.3.

```auto

$headers = "From: $name_field <$email_field>
";
$headers .= "Reply-To: $name_field <$email_field>
";

mail($to, $subject, $body, **[COLOR=Blue]$headers[/COLOR]**);

```

[/quote]

---

<div class="post-metadata">

**Author:** ![mikeepic](https://avatars.discourse-cdn.com/v4/letter/m/2bfe46/32.png) [@mikeepic](https://forum.kirupa.com/u/mikeepic)\
**Post date:** [May 11, 2008, 6:10pm UTC](https://forum.kirupa.com/t/php-contact-form-please-help-w-minor-tweaks/259986/4 "2008-05-11T18:10:50Z")

</div>

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 &lt;$email_field&gt;

```

";  
$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.

---

<div class="post-metadata">

**Author:** ![kitsunegari](https://avatars.discourse-cdn.com/v4/letter/k/eb8c5e/32.png) [@kitsunegari](https://forum.kirupa.com/u/kitsunegari)\
**Post date:** [May 12, 2008, 7:08am UTC](https://forum.kirupa.com/t/php-contact-form-please-help-w-minor-tweaks/259986/5 "2008-05-12T07:08:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mikeepic](https://avatars.discourse-cdn.com/v4/letter/m/2bfe46/32.png) [@mikeepic](https://forum.kirupa.com/u/mikeepic)\
**Post date:** [May 13, 2008, 4:09am UTC](https://forum.kirupa.com/t/php-contact-form-please-help-w-minor-tweaks/259986/6 "2008-05-13T04:09:14Z")

</div>

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 &lt;$email_field&gt;

```

";  
$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.

---

<div class="post-metadata">

**Author:** ![kitsunegari](https://avatars.discourse-cdn.com/v4/letter/k/eb8c5e/32.png) [@kitsunegari](https://forum.kirupa.com/u/kitsunegari)\
**Post date:** [May 13, 2008, 5:35am UTC](https://forum.kirupa.com/t/php-contact-form-please-help-w-minor-tweaks/259986/7 "2008-05-13T05:35:08Z")

</div>

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.

```auto
<?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';
}
?>

```

---

<div class="post-metadata">

**Author:** ![djheru](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/djheru/32/3190_2.png) [@djheru](https://forum.kirupa.com/u/djheru)\
**Post date:** [May 13, 2008, 1:18pm UTC](https://forum.kirupa.com/t/php-contact-form-please-help-w-minor-tweaks/259986/8 "2008-05-13T13:18:44Z")

</div>

As kitsunegari said, isset($\_POST[‘name’]) only checks to see if the KEY exists, a better way might be:

```php

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:

```auto

*
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;
    }
}*

```

---

<div class="post-metadata">

**Author:** ![mikeepic](https://avatars.discourse-cdn.com/v4/letter/m/2bfe46/32.png) [@mikeepic](https://forum.kirupa.com/u/mikeepic)\
**Post date:** [May 13, 2008, 5:52pm UTC](https://forum.kirupa.com/t/php-contact-form-please-help-w-minor-tweaks/259986/9 "2008-05-13T17:52:34Z")

</div>

Thanks to both of you for your help and patience. It’s becoming a bit clearer now. Big help!
