Jump to content

Creating html/php email form


wolfsta

Recommended Posts

Hi all

 

New here.

 

I have a basic email form that takes name phone email etc, also has 3 file upload boxes for photos.

 

I have taken most of the code from free examples off the net and patched it together.

 

It had an error message if something went wrong. I wanted to put an html page instead of just the txt. So read that I could end tag the php then do html then begin tag the php again. Problem is some of the error message was from a different part of the php code. How can i get this displayed on the html.

 


<?php 
    if(isset($_POST['email'])) { 
         
        // EDIT THE 2 LINES BELOW AS REQUIRED 
        $email_to = "email@email.com"; 
        $email_subject = "JoinUs Model Form"; 
         
         
        function died($error) { 
            // your error code can go here 
            ?> 
             
            <title>title</title> 
            <style type="text/css"> 
            body { 
                background-color: #000; 
                text-align: center; 
            } 
            body,td,th { 
                color: #FFF; 
                font-family: Arial, Helvetica, sans-serif; 
                font-size: 24px; 
            } 
            </style> 
            </head> 
            <body link="#FFFFFF" onload="setTimeout('history.back()',10000)"> 
            <p><img src="../images/logo.png" width="326" height="144" alt="sdadfsd" longdesc="http://www.dgfsfdg.com" /><br /> 
            </p> 
            <br /> 
            <p>We are very sorry, but there were error(s) found with the form you submitted.</p> 
            <p>These errors appear below.</p> 
             
       ####### this is where i want the below errors to appear in same format as html ######### 
             
            <p>Click back to fix your error(s) or you will be taken back to the form automatically in 10 seconds...</p> 
            <h6> </h6> 
            <h6>© asafdsodf  2012</h6> 
            </body> 
            </html> 
             
            <?php 
            die(); 
        } 
         
        // validation expected data exists 
        if(!isset($_POST['first_name']) || 
           !isset($_POST['last_name']) || 
           !isset($_POST['email']) || 
           !isset($_POST['telephone']) || 
           !isset($_POST['comments'])) { 
            died('We are sorry, but there appears to be a problem with the form you submitted.');        
        } 
         
        // Where the file is going to be placed 
        $target_path = "uploads/"; 
         
        /* Add the original filename to our target path.   
        Result is "uploads/filename.extension" */ 
        $target_path = $target_path . basename( $_FILES['form2']['photo1']); 
        $target_path = $target_path . basename( $_FILES['form2']['photo2']); 
        $target_path = $target_path . basename( $_FILES['form2']['photo3']); 
         
        $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 
        $age = $_POST['age']; 
        $city = $_POST['city']; 
        $state = $_POST['state']; 
        $height_feet = $_POST['height_feet']; 
        $height_inches = $_POST['height_inches']; 
        $photo1 = $_FILES['form2']['photo1']; 
        $photo2 = $_FILES['form2']['photo2']; 
        $photo3 = $_FILES['form2']['photo3']; 
         
        $error_message = ""; 
        $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/'; 
        if(!preg_match($email_exp,$email_from)) { 
            $error_message .= 'The Email Address you entered does not appear to be valid.<br />'; 
        } 
        $string_exp = "/^[A-Za-z .'-]+$/"; 
        if(!preg_match($string_exp,$first_name)) { 
            $error_message .= 'The First Name you entered does not appear to be valid.<br />'; 
        } 
        if(!preg_match($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 />'; 
        } 
        if(strlen($error_message) > 0) { 
            died($error_message); 
        } 
        $email_message = "Form details below.\n\n"; 
         
        function clean_string($string) { 
            $bad = array("content-type","bcc:","to:","cc:","href"); 
            return str_replace($bad,"",$string); 
        } 
         
        $email_message .= "First Name: ".clean_string($first_name)."\n"; 
        $email_message .= "Last Name: ".clean_string($last_name)."\n"; 
        $email_message .= "Age: ".clean_string($age)."\n"; 
        $email_message .= "Height: ".clean_string($height_feet) .clean_string($height_inches)."\n"; 
        $email_message .= "City: ".clean_string($city)."\n"; 
        $email_message .= "State: ".clean_string($state)."\n"; 
        $email_message .= "Email: ".clean_string($email_from)."\n"; 
        $email_message .= "Telephone: ".clean_string($telephone)."\n"; 
        $email_message .= "Comments: ".clean_string($comments)."\n"; 
        $email_message .= "".clean_string($comments)."\n"; 
        $email_message .= "Comments: ".clean_string($comments)."\n"; 
        $email_message .= "Comments: ".clean_string($comments)."\n"; 
         
         
        // 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);   
    ?> 

<!-- include your own success html here --> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
<title>asdf </title> 
<style type="text/css"> 
body { 
    background-color: #000; 
    text-align: center; 
} 
body,td,th { 
    color: #FFF; 
    font-family: Arial, Helvetica, sans-serif; 
    font-size: 24px; 
} 
</style> 
</head> 

<body link="#FFFFFF"> 
<p><img src="./images/logo.png" width="326" height="144" alt="asdfahjdsfka" longdesc="http://www.kasdfhdskaha.com" /><br /> 
</p> 
<p>Thank your for applying to adskfha asdfasd.  We will be in touch with you very soon.</p> 
<p>You will be redirected back to the site in 3 seconds...</p> 
<h6> </h6> 
<h6>© 2012</h6> 
</body> 
</html> 

<?php 
    } 
    ?>

 

 

Thanks

 

Wolfsta  8)

Link to comment
Share on other sites

if never seen the use of died, just die.  but to use this line as an example :

died('We are sorry, but there appears to be a problem with the form you submitted.'); 

You can apply html tags to the message being returned from the php, so it could be done like :

died('<span style="color:red;text-decorate:underline;">We are sorry, but there appears to be a problem with the form you submitted.</span>'); 

 

Is that what your after, 'cause I'm not too sure.

Link to comment
Share on other sites

Sorry let me explain a little more.. this is the original code that i got from a website.  I started messing with it. 

 


<?php
if(isset($_POST['email'])) {
     
    // EDIT THE 2 LINES BELOW AS REQUIRED
    $email_to = "you@yourdomain.com";
    $email_subject = "Your email subject line";
     
     
    function died($error) {
        // your error code can go here
        echo "We are very sorry, but there were error(s) found with the form you submitted. ";
        echo "These errors appear below.<br /><br />";
        echo $error."<br /><br />";
        echo "Please go back and fix these errors.<br /><br />";
        die();
    }
     
    // validation expected data exists
    if(!isset($_POST['first_name']) ||
        !isset($_POST['last_name']) ||
        !isset($_POST['email']) ||
        !isset($_POST['telephone']) ||
        !isset($_POST['comments'])) {
        died('We are sorry, but there appears to be a problem with the form you submitted.');       
    }
     
    $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-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
  if(!preg_match($email_exp,$email_from)) {
    $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
  }
    $string_exp = "/^[A-Za-z .'-]+$/";
  if(!preg_match($string_exp,$first_name)) {
    $error_message .= 'The First Name you entered does not appear to be valid.<br />';
  }
  if(!preg_match($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 />';
  }
  if(strlen($error_message) > 0) {
    died($error_message);
  }
    $email_message = "Form details below.\n\n";
     
    function clean_string($string) {
      $bad = array("content-type","bcc:","to:","cc:","href");
      return str_replace($bad,"",$string);
    }
     
    $email_message .= "First Name: ".clean_string($first_name)."\n";
    $email_message .= "Last Name: ".clean_string($last_name)."\n";
    $email_message .= "Email: ".clean_string($email_from)."\n";
    $email_message .= "Telephone: ".clean_string($telephone)."\n";
    $email_message .= "Comments: ".clean_string($comments)."\n";
     
     
// 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);  
?>

<!-- include your own success html here -->

Thank you for contacting us. We will be in touch with you very soon.

<?php
}
?>

 

Instead of just displaying a error in plain text.  I want it to display nicely on a html page with logo and everything then the errors and also redirect back to the form after say 10seconds.

 

I took the error section and ended the php tag.. inserted html.. and then started the php at the end again as below:

 


function died($error) {
            // your error code can go here

		?>
            
		<title>asdf adsf</title>
		<style type="text/css">
		body {
			background-color: #000;
			text-align: center;
		}
		body,td,th {
			color: #FFF;
			font-family: Arial, Helvetica, sans-serif;
			font-size: 24px;
		}
		</style>
		</head>
		<body link="#FFFFFF" onload="setTimeout('history.back()',10000)">
		<p><img src="../images/logo.png" width="326" height="144" alt="asdfa" longdesc="http://www.adsfadsf.com" /><br />
		</p>
            <br />
		<p>We are very sorry, but there were error(s) found with the form you submitted.</p>
            <p>These errors appear below.</p>
            
            
		<p>Click back to fix your error(s) or you will be taken back to the form automatically in 10 seconds...</p>
		<h6> </h6>
		<h6>© asdf 2012</h6>
		</body>
		</html>
            
            <?php
            die();
        }

 

But i realised that the errors were generated from the php so can you provide either and easier way to display a full page and logo when it does the errors, or b inserted the errors via php into the html section.

 

Hope I'm explaining this clearly.

 

Thanks

 

Wolfsta

Link to comment
Share on other sites

Thanks after what you said it made me go back and look. 

 

I just needed to add this in the middle of my html to get it to display.

<?php
echo $error."<br /><br />";
?>

 

But I will go back and tidy up into an html file like you suggested after.

 

Thanks guys!

 

Wolfsta

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.