# Additional Conditions to Contact Form?

**URL:** <https://forum.kirupa.com/t/additional-conditions-to-contact-form/262564>\
**Category:** programming\
**Created:** [June 6, 2008, 9:59pm UTC](https://forum.kirupa.com/t/additional-conditions-to-contact-form/262564 "2008-06-06T21:59:28Z")\
**Posts on this page:** 4\
**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:** [June 6, 2008, 9:59pm UTC](https://forum.kirupa.com/t/additional-conditions-to-contact-form/262564/1 "2008-06-06T21:59:28Z")

</div>

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](http://www.mgmtransformer/thankyou.html)” );  
mail($to, $subject, $body, $headers);

}  
elseif (!$\_POST[‘name’]) echo ‘name is required’;  
elseif (!$\_POST[‘email’]) echo ‘email is required’;

?\>

---

<div class="post-metadata">

**Author:** ![sekasi](https://avatars.discourse-cdn.com/v4/letter/s/5f8ce5/32.png) [@sekasi](https://forum.kirupa.com/u/sekasi)\
**Post date:** [June 6, 2008, 11:29pm UTC](https://forum.kirupa.com/t/additional-conditions-to-contact-form/262564/2 "2008-06-06T23:29:42Z")

</div>

```php

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

```

---

<div class="post-metadata">

**Author:** ![sekasi](https://avatars.discourse-cdn.com/v4/letter/s/5f8ce5/32.png) [@sekasi](https://forum.kirupa.com/u/sekasi)\
**Post date:** [June 6, 2008, 11:30pm UTC](https://forum.kirupa.com/t/additional-conditions-to-contact-form/262564/3 "2008-06-06T23:30:15Z")

</div>

edit, might want to put in a else { } after that last elseif, just for code validation…

---

<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:** [June 7, 2008, 12:44am UTC](https://forum.kirupa.com/t/additional-conditions-to-contact-form/262564/4 "2008-06-07T00:44:32Z")

</div>

In the if conditional, I like to use

```php

     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

```php
if($_POST['name'])

```

It will still evaluate to true.
