PDA

View Full Version : Mail Form


Crumpy1
01-24-2012, 02:25 PM
I am looking to edit my php code to make my own webpages display my thank you message when people submit a form from my website.

I have attached the code of my current form, however these only produce basic text screens and i would like them to be redirected to a pre-made webpage so it doesnt look so tackey. I am new to php so i have used a template to do this.

I have another form on my website which will require a copy of the submited form back to the sender via email, but again do not know where in this code it goes.

Any Help would be much appreciated.

Thanks

edbr
01-25-2012, 12:13 AM
where you have
if ( mail($to, $subjectline, $body, $security, "-f".$email)) {
echo $thanks;
} else {
echo "The email failed to send! Please try again later"; // Message If mail not sent
}

change to edit to the page you want
if ( mail($to, $subjectline, $body, $security, "-f".$email)) {
header('Location: http://www.example.com/thanks.html');
} else {
echo "The email failed to send! Please try again later"; // Message If mail not sent
}

Crumpy1
01-25-2012, 08:22 AM
Thank you for that, it works perfectly. If i wanted to email a copy of the reply to the sender where would i put that in the code? i am assuming it would go here $to='admin@localhost'; but do not know how to do an & in PHP. The text field id for where they type their email is 'email' incase you need to know.

Crumpy1
01-25-2012, 11:34 AM
Sorry to be a pain, but now i have added validation to my form this code has caused me even more errors. it runs the code below which forces users to complete the non mandatory fields before submitting. How can i delete this section? i have tried deleting the code after 'else' but that produces a white page on submit. This is also the form where the response will need to go to myself and the sender.

if any1 can help i would be extremeley appreciative.

Thanks

// The ['email'] should match the name= in the email text input box in your form. Used for email validation.

// DO NOT CHANGE THIS HERE. Change your form to match this.

$email = $_POST['email'] ; // collect users email from form for the email header.

// Collect all information from form and check all fields have been filled in.

if (sizeof($_POST)) {

$body = "";

while(list($key, $val) = each($_POST)) {

if ($key == "Submit") {

//do nothing

}

else {

$body .= "$key:\n $val\r\n";

// Checks if $val contains data

if(empty($val)) {

echo ("<b><p><li>One or more required fields have not been filled in.</li></p>

<p><li>Please go <a href='javascript: history.go(-1)'>Back</a> and try again</li></p></b>");

exit();

}

}

}}

edbr
01-26-2012, 01:48 AM
post the code what you have now its not clear to me at the moment

Crumpy1
01-26-2012, 08:09 AM
post the code what you have now its not clear to me at the moment

The code as it is atm is below. Thanks!

<?

error_reporting(E_ALL);

/************************************************** ****

******** Secure mailing script *****************

******** Copyright David Wilde *****************

************************************************** *****

You can add your template to this page.

Make your form action value the absolute or relative path to the location of this script once it is uploaded to your site.

Example;

Absolute path: <form method="post" action="http://www.yourdomain.com/scripts/mailprocess.php">

OR relative path: <form method="post" action="scripts/mailprocess.php">

Assuming you have a scripts folder and you upload the script to that folder.

The script Checks that all fields contain information and validates the email address.

You must have the email text field in your form set with the name of 'email' the name is case sensative, so make sure it is in lowercase.

*/

################################################## #################################

// Start config variables

// ONLY CHANGE THE DETAILS IN BETWEEN THE QUOTES; E.G 'your@email-address.com'

//

$to='admin@localhost'; // You must change this! This should be the email address you want the form information sent to.

//

$subjectline='Web site form - Contact Us'; // The email subject line. You may leave this as is.

//

################################################## ##################################

## Do Not Edit Below this Line ##

################################################## ##################################

//

$thanks="<h1> We have received your message.</h1><br />

<p>We will deal with it as soon as possible</p> <p><ul><li>Go <a href='/'>Back</a> back to the Home Page/li></ul>"; // This is the message given after successfull submission of form.

// The ['email'] should match the name= in the email text input box in your form. Used for email validation.

// DO NOT CHANGE THIS HERE. Change your form to match this.

$email = $_POST['email'] ; // collect users email from form for the email header.

// Collect all information from form and check all fields have been filled in.

if (sizeof($_POST)) {

$body = "";

while(list($key, $val) = each($_POST)) {

if ($key == "Submit") {

//do nothing

}

else {

$body .= "$key:\n $val\r\n";

// Checks if $val contains data

if(empty($val)) {

echo ("<b><p><li>One or more required fields have not been filled in.</li></p>

<p><li>Please go <a href='javascript: history.go(-1)'>Back</a> and try again</li></p></b>");

exit();

}

}

}}

// Validate email address

if(!preg_match("/^[a-z0-9]+([_\\.-][a-z0-9]+)*" ."@"."([a-z0-9]+([\.-][a-z0-9]+)*)+"."\\.[a-z]{2,}"."$/",$email)){

echo ("<b><li>Invalid email address entered. Go <a href='javascript: history.go(-1)'>Back</a> and try again</li></b>");

exit();

}

// Clean up email address

if (get_magic_quotes_gpc()){

$email = stripslashes($email);

}

// Set headers

$security = "From: ".$email."\r\n";

$security .= "Reply-To: ".$email."\r\n";

$security .= "Return-Path: \r\n";

$security .= "CC: \r\n";

$security .= "BCC: \r\n";

ini_set("sendmail_from", $email);

// Send the email.

if ( mail($to, $subjectline, $body, $security, "-f".$email)) {

header('Location:http://localhost\HFCC\Email_Responses\email_App_Response. html');

} else {

header('Location:http://localhost/HFCC/Email_Responses/failed.html'); // Message If mail not sent

}

?>

Corrosive
01-26-2012, 08:25 AM
Hi Crumpy1. We don't mind you posting code but please wrap it in code tags (the # symbol at the top of the text editor) to keep it nice and neat. Thank you - your friendly admin :)

edbr
01-26-2012, 08:31 AM
i havent tested this as im running late , but try this I simplyfied it , ionly left email validation in it. If it fails ill look tomorrow or maybe corrosive will help look for unbalanced braces etc

<?php

error_reporting(E_ALL);

/************************************************** ****

******** Secure mailing script *****************

******** Copyright David Wilde *****************

************************************************** *****

You can add your template to this page.

Make your form action value the absolute or relative path to the location of this script once it is uploaded to your site.

Example;

Absolute path: <form method="post" action="http://www.yourdomain.com/scripts/mailprocess.php">

OR relative path: <form method="post" action="scripts/mailprocess.php">

Assuming you have a scripts folder and you upload the script to that folder.

The script Checks that all fields contain information and validates the email address.

You must have the email text field in your form set with the name of 'email' the name is case sensative, so make sure it is in lowercase.

*/

################################################## #################################

// Start config variables

// ONLY CHANGE THE DETAILS IN BETWEEN THE QUOTES; E.G 'your@email-address.com'

//

$to='admin@localhost'; // You must change this! This should be the email address you want the form information sent to.

//

$subjectline='Web site form - Contact Us'; // The email subject line. You may leave this as is.

//

################################################## ##################################

## Do Not Edit Below this Line ##

################################################## ##################################

//

$thanks="<h1> We have received your message.</h1><br />

<p>We will deal with it as soon as possible</p> <p><ul><li>Go <a href='/'>Back</a> back to the Home Page/li></ul>"; // This is the message given after successfull submission of form.

// The ['email'] should match the name= in the email text input box in your form. Used for email validation.

// DO NOT CHANGE THIS HERE. Change your form to match this.

$email = $_POST['email'] ; // collect users email from form for the email header.

// Collect all information from form and check all fields have been filled in.


// Validate email address

if(!preg_match("/^[a-z0-9]+([_\\.-][a-z0-9]+)*" ."@"."([a-z0-9]+([\.-][a-z0-9]+)*)+"."\\.[a-z]{2,}"."$/",$email)){

echo ("<b><li>Invalid email address entered. Go <a href='javascript: history.go(-1)'>Back</a> and try again</li></b>");

exit();

}

// Clean up email address

if (get_magic_quotes_gpc()){

$email = stripslashes($email);

}

// Set headers

$security = "From: ".$email."\r\n";

$security .= "Reply-To: ".$email."\r\n";

$security .= "Return-Path: \r\n";

$security .= "CC: \r\n";

$security .= "BCC: \r\n";

ini_set("sendmail_from", $email);

// Send the email.

if ( mail($to, $subjectline, $body, $security, "-f".$email)) {

header('Location:http://localhost\HFCC\Email_Responses\email_App_Response. html');

} else {

header('Location:http://localhost/HFCC/Email_Responses/failed.html'); // Message If mail not sent

}

?>

Crumpy1
01-26-2012, 08:51 AM
Hi Crumpy1. We don't mind you posting code but please wrap it in code tags (the # symbol at the top of the text editor) to keep it nice and neat. Thank you - your friendly admin :)

I will do in future. Sorry about that i did not realise that was how it was done.

Crumpy1
01-26-2012, 08:53 AM
i havent tested this as im running late , but try this I simplyfied it , ionly left email validation in it. If it fails ill look tomorrow or maybe corrosive will help look for unbalanced braces etc

<?php

error_reporting(E_ALL);

/************************************************** ****

******** Secure mailing script *****************

******** Copyright David Wilde *****************

************************************************** *****

You can add your template to this page.

Make your form action value the absolute or relative path to the location of this script once it is uploaded to your site.

Example;

Absolute path: <form method="post" action="http://www.yourdomain.com/scripts/mailprocess.php">

OR relative path: <form method="post" action="scripts/mailprocess.php">

Assuming you have a scripts folder and you upload the script to that folder.

The script Checks that all fields contain information and validates the email address.

You must have the email text field in your form set with the name of 'email' the name is case sensative, so make sure it is in lowercase.

*/

################################################## #################################

// Start config variables

// ONLY CHANGE THE DETAILS IN BETWEEN THE QUOTES; E.G 'your@email-address.com'

//

$to='admin@localhost'; // You must change this! This should be the email address you want the form information sent to.

//

$subjectline='Web site form - Contact Us'; // The email subject line. You may leave this as is.

//

################################################## ##################################

## Do Not Edit Below this Line ##

################################################## ##################################

//

$thanks="<h1> We have received your message.</h1><br />

<p>We will deal with it as soon as possible</p> <p><ul><li>Go <a href='/'>Back</a> back to the Home Page/li></ul>"; // This is the message given after successfull submission of form.

// The ['email'] should match the name= in the email text input box in your form. Used for email validation.

// DO NOT CHANGE THIS HERE. Change your form to match this.

$email = $_POST['email'] ; // collect users email from form for the email header.

// Collect all information from form and check all fields have been filled in.


// Validate email address

if(!preg_match("/^[a-z0-9]+([_\\.-][a-z0-9]+)*" ."@"."([a-z0-9]+([\.-][a-z0-9]+)*)+"."\\.[a-z]{2,}"."$/",$email)){

echo ("<b><li>Invalid email address entered. Go <a href='javascript: history.go(-1)'>Back</a> and try again</li></b>");

exit();

}

// Clean up email address

if (get_magic_quotes_gpc()){

$email = stripslashes($email);

}

// Set headers

$security = "From: ".$email."\r\n";

$security .= "Reply-To: ".$email."\r\n";

$security .= "Return-Path: \r\n";

$security .= "CC: \r\n";

$security .= "BCC: \r\n";

ini_set("sendmail_from", $email);

// Send the email.

if ( mail($to, $subjectline, $body, $security, "-f".$email)) {

header('Location:http://localhost\HFCC\Email_Responses\email_App_Response. html');

} else {

header('Location:http://localhost/HFCC/Email_Responses/failed.html'); // Message If mail not sent

}

?>

Thank you for the revised code, i will try this later and repost tommorow! Thank you so much for your help it is genuinly appreciated.

Crumpy1
01-27-2012, 02:37 PM
thank you for the code, i have tried it today but i am getting the object not found error. I have setup the php file process correctly in Dreamweaver but i am unsure why. Is there any codes available to download, as i am a student and have not covered php yet so i am finding the coding side difficult at the moment. All as i want my php to do is submit my form (which is already validated in the html code) to be sent to an email address of the sender and to me. Then go to the failed to send or successfully sent webpage page. Thanks to you guys explaining the Location: field etc this part does work for my 1 form (as all fields are mandatory) but the This template is one i used from the college but it checks validation etc and forces the users to fill in all fields including the non mandatory ones. All of my validation has been done manually by me in dreamweaver.

Thank you for your help again.

edbr
01-27-2012, 11:52 PM
ok sorry i had a feeling i rushed to much, if you give me a whil ill look again and recode for you

Crumpy1
01-29-2012, 08:37 PM
ok sorry i had a feeling i rushed to much, if you give me a whil ill look again and recode for you

Thank you for all your help, however if you do not wish to fix my code, if you know how are where to download free codes that i can use that will also be fine.

Thanks Again

edbr
01-30-2012, 07:43 AM
here's a script you can try and implement, it has some validation for email and some security for url and input cleaning.
it will send a copy to the email entered in the form. i left the example on 1 page for ease but you could split the form and use the action to point to the script on another page.


<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>


<style type="text/css">
label{ width:160px;position:relative; float:left;clear:left; margin-bottom:5px;}
input{ width:160px; position:relative; float:left; margin-bottom:5px;}
textarea{ width:160px; height:160px; position:relative; float:left; margin-bottom:5px; }
.button { border: 1px solid #006; margin-left:160px; background: #ccf;clear:left;}
.button:hover { border: 1px solid #f00; background: #eef; margin-left:160px;}
.row{ width:600px; clear:both; position:relative; float:left; margin-bottom:5px;}
</style>
</head>

<body>
<div class="row">
<form action="" method="post">




<label> email</label><input name="email" type="text">
<label> first tname</label><input name="first_name" type="text">
<label>last name</label><input name="last_name" type="text">
<label>telephone</label><input name="telephone" type="text">
<label>comments</label><textarea name="comments" cols="8" rows="8"></textarea>
<input name="Send" class="button" type="submit" value="Submit">

</form>

<?php

if(isset($_POST['email'])) {

// EDIT THE BELOW
$email_to = "mail@domain.com";
$subject = "Your email subject line";

//edit to exact pathe of mail script
$url = "http://www.domain.com/email/test.php";

// Do not edit
$referer = $_SERVER['HTTP_REFERER'];

if ($referer != $url) { echo "You do not have permission to use this script from another URL.";
exit; }



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();
}

// edit required post fields in format below
if(empty($_POST['first_name']) ||
empty($_POST['last_name']) ||
empty($_POST['email']) ||
empty($_POST['telephone']) ||
empty($_POST['comments'])) {
died('some of the required fields are empty.');
}

function clean($n){
$n = trim($n);
$n = stripslashes($n);
$n = htmlentities($n);
return $n;
}

$first_name = clean( $_POST['first_name']); // required
$last_name = clean($_POST['last_name'] ); // required
$email_from = clean( $_POST['email']); // required
$telephone = clean($_POST['telephone']); // not required
$comments = clean($_POST['comments']); // required

$error = "";


$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
echo $error."<br /><br />";
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}


if(strlen($error) > 0) {
died($error_message);
}
$message = "Form details below.\n\n";

function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}

$referer = $_SERVER['HTTP_REFERER'];
if ($referer != $url) {

echo "You do not have permission to use this script from another URL.";

exit;

}

$message .= "First Name: ".$first_name."\n";
$message .= "Last Name: ".$last_name."\n";
$message .= "Email: ".$email_from."\n";
$message .= "Telephone: ".$telephone."\n";
$message .= "Comments: ".$comments."\n";


if(strlen($error) < 1) {

// create email headers
$headers = 'From: '.$email_to."\r\n";
$headers .= 'Reply-To: '.$email_to."\r\n" ;
$headers .= 'X-Mailer: PHP/' . phpversion();
@mail($email_to, $subject, $message, $headers);
@mail($email_from, $subject, $message, $headers);
}?>
</div>
<div class="row">
<!-- include your own success html here -->
<br />
<h1>Thank you for contacting us. We will be in touch with you very soon.</h1>
</div>
<?php
}
?>
</body>
</html>

Crumpy1
01-30-2012, 12:14 PM
Thank you for the code, i will update my form with your code and post back with how i get on.

Many Many Many Thanks!

edbr
01-31-2012, 12:17 AM
wait to hear

Crumpy1
01-31-2012, 08:03 AM
I have tested the code with my forms, when i click submit the following message appears:
"We are very sorry, but there were error(s) found with the form you submitted. These errors appear below.

some of the required fields are empty.

Please go back and fix these errors"

(The 5 fields First Name etc are also onscreen)

All form fields are completed. Am i doing something obviously wrong? or do i have to redo the whole form again in the code and use that as the form?

edbr
01-31-2012, 08:18 AM
check field names in your form math exactly the reserved ones i laid out , or delete the ones you dont want

if(empty($_POST['first_name']) ||
empty($_POST['last_name']) ||
empty($_POST['email']) ||
empty($_POST['telephone']) ||
empty($_POST['comments'])) {
died('some of the required fields are empty.');
}
so if you only want email

if( empty($_POST['email'])
) {
died('some of the required fields are empty.');
}

Crumpy1
01-31-2012, 08:38 AM
The URL for the form i am trying to submit is http://hfcc.0catch.com/apply/index.html .

I have amended the code for email only but upon completion it is still displaying the same message but only providing the email box.

is making the user retype the email address at the end the best way to send them a copy?

Thank you for your patients.

Crumpy1
01-31-2012, 04:04 PM
I have finally got the code working for 2 out of the 3 forms. The 1 form keeps producing 'object not found error 404' on submit. The form is in the same root folder as the other pages and share the same php code however when the others say sent sucsessfully this one gives the error message. Are there any obvious things with this error? The location of the php file and webpages are 100% definetly correct.

Crumpy1
01-31-2012, 04:47 PM
I have now got all 3 working without the Location: .... part at the end however when i put my webpages as the failed and confirmation the object not found error occurs error 404. Could you please scan my text below to see for any obvious errors. I am using a localhost xammp should that be relevant.

'
// Send the email.

if ( mail($to, $subjectline, $body, $security, "-f".$email)) {

header('Location:http:\\localhost\HFCC\Email_Respo nses\Contact_App_Response. html'); }

else {

header('Location:http:\\localhost\HFCC\Email_Respo nses\failed.html'); // Message If mail not sent

}

?>'

Crumpy1
01-31-2012, 04:53 PM
Whole code is below incase that part is relevant.

<?

error_reporting(E_ALL);

/************************************************** ****

******** Secure mailing script *****************

******** Copyright David Wilde *****************

************************************************** *****

You can add your template to this page.

Make your form action value the absolute or relative path to the location of this script once it is uploaded to your site.

Example;

Absolute path: <form method="post" action="http://www.yourdomain.com/scripts/mailprocess.php">

OR relative path: <form method="post" action="scripts/mailprocess.php">

Assuming you have a scripts folder and you upload the script to that folder.

The script Checks that all fields contain information and validates the email address.

You must have the email text field in your form set with the name of 'email' the name is case sensative, so make sure it is in lowercase.

*/

################################################## #################################

// Start config variables

// ONLY CHANGE THE DETAILS IN BETWEEN THE QUOTES; E.G 'your@email-address.com'

//

$to='admin@localhost'; // You must change this! This should be the email address you want the form information sent to.

//

$subjectline='Web site form - Feedback'; // The email subject line. You may leave this as is.

//

################################################## ##################################

## Do Not Edit Below this Line ##

################################################## ##################################

//

$thanks="<h1>Thank you for submitting the form.</h1><br />

<p>We will deal with it as soon as possible</p> <p><ul><li>Go <a href='/'>Back</a> back to the Home Page/li></ul>"; // This is the message given after successfull submission of form.

// The ['email'] should match the name= in the email text input box in your form. Used for email validation.

// DO NOT CHANGE THIS HERE. Change your form to match this.

$email = $_POST['email'] ; // collect users email from form for the email header.

// Collect all information from form and check all fields have been filled in.

if (sizeof($_POST)) {

$body = "";

while(list($key, $val) = each($_POST)) {

if ($key == "Submit") {

//do nothing

}

else {

$body .= "$key:\n $val\r\n";

// Checks if $val contains data

if(empty($val)) {

/*echo ("<b><p><li>One or more required fields have not been filled in.</li></p>

<p><li>Please go <a href='javascript: history.go(-1)'>Back</a> and try again</li></p></b>");

exit();*/

}

}

}}

// Validate email address

/*if(!eregi("^[a-z0-9]+([_\\.-][a-z0-9]+)*" ."@"."([a-z0-9]+([\.-][a-z0-9]+)*)+"."\\.[a-z]{2,}"."$",$email)){

echo ("<b><li>Invalid email address entered. Go <a href='javascript: history.go(-1)'>Back</a> and try again</li></b>");

exit();

}*/

// Clean up email address

if (get_magic_quotes_gpc()){

$email = stripslashes($email);

}

// Set headers

$security = "From: ".$email."\r\n";

$security .= "Reply-To: ".$email."\r\n";

$security .= "Return-Path: \r\n";

$security .= "CC: \r\n";

$security .= "BCC: \r\n";

ini_set("sendmail_from", $email);

// Send the email.

if ( mail($to, $subjectline, $body, $security, "-f".$email)) {

header('Location:http:\\localhost\HFCC\Email_Respo nses\Contact_App_Response. html'); }

else {

header('Location:http:\\localhost\HFCC\Email_Respo nses\failed.html'); // Message If mail not sent

}

?>

Crumpy1
02-01-2012, 07:58 AM
Many Many Thanks to edbr for all your help, i have finally got the code to work correctly.

Your help has been much appreciated.