# PHP Email Form Help



## Karamas (Feb 16, 2011)

I'm working on finishing up the template for my portfolio website and need to use PHP to create an email form.  I found many examples but, I am not familiar with PHP as to asp.net (which my host does not use).  

The code sends emails to my inbox if all of the required fields are filled out and meet validation.  But when an error occurs from a requirement not being met, it loads a new page explaining what the error is.  (You can test this here.)  I would rather have a red text warning to pop up next to the required field in which the validation failed.  The same thing also happens when the email submission was successful.  I would rather a small text box show saying the email was successful or something like that rather than another plank page loading.  

This is the code I am currently using.  The php file is external (not embedded).

I would greatly appreciate any help people are able to give.   

Here is the html code I have so far where the php file is called.


```
<div id="my_contact_form">
                <form name="contact_form" method="post" action="php_files/email_form.php">
                    <label for="first_name">First Name*</label>
                    <label for="first_name_error" style="color: "Red"/><br />
                    <input  type="text" name="first_name" maxlength="50" size="30" /><br />
                    
                    <label for="last_name">Last Name*</label><br />
                    <input  type="text" name="last_name" maxlength="50" size="30" /><br />
                    
                    <label for="email">Email Address*</label><br />
                    <input  type="text" name="email" maxlength="80" size="30" /><br />
                    
                    <label for="telephone">Phone Number</label><br />
                    <input  type="text" name="telephone" maxlength="30" size="30" /><br />
                    
                    <br />
                    
                    <label for="subject">Subject</label><br />
                    <input type="text" name="subject" maxlength="100" size="30" /><br />
                    
                    <label for="comments">Comments*</label><br />
                    <textarea  name="comments" cols="30" rows="6" ></textarea><br />
                    <p style="color: Red;">* Indicates a required field</p><br />
                    
                    <input type="submit" value="Submit" />
                </form>
            </div>
```

And, here is my external php file.


```
<?php

// Most of the Php Email form was aquired from.....    
// http://www.freecontactform.com/email_form.php
// Thanks for the help.


if(isset($_POST['email'])) {
     
    $email_to = "bcornett24@gmail.com";
    
    $email_subject = $_POST['subject']; // not required
    
    if($email_subject == "") {
		$email_subject = "READ NOW: This is an email from my website.";
    }
     
    $first_name = $_POST['first_name']; // required
    $last_name = $_POST['last_name']; // required
    $email_from = $_POST['email']; // required
    $telephone = $_POST['telephone']; // not required
    $comments = $_POST['comments']; // required
     
    $error_message = "";
    
    $email_exp = "^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$";
  if(!eregi($email_exp,$email_from)) {
    $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
  }
    $string_exp = "^[a-z .'-]+$";
  if(!eregi($string_exp,$first_name)) {
    $error_message = "The First Name you entered does not appear to be valid.";
    $_POST['first_name_error'] = $error_message;
    
  }
  if(!eregi($string_exp,$last_name)) {
    $error_message .= 'The Last Name you entered does not appear to be valid.<br />';
  }
  if(strlen($comments) < 2) {
    $error_message .= 'The Comments you entered do not appear to be valid.<br />';
  }
  
     
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers); 
?>
 
<?php
}
?>
```


----------



## W1zzard (Feb 16, 2011)

need to use javascript for that

handle the form onsubmit message with your js code


----------



## Karamas (Feb 16, 2011)

W1zzard said:


> need to use javascript for that
> 
> handle the form onsubmit message with your js code



I do know javaScript, will it matter if I use embedded or external js file?


----------

