...

View Full Version : Tweaking php action page so all fields appear in e-mail



Kevin_M_Schafer
01-23-2012, 04:40 PM
I found a php and html comment form online. I'm new to php, and so far I have been able to make the form work. This is interesting. I have added two fields to the php page and to the form.

Right now, when the form is sent, all that appears in the "Web master" e-mail is the comment field information. What do I need to change or add so that all the fields and their content show up in the e-mail that is sent to me?

I would like the comment to appear, the sender's name, their telephone number, and their e-mail address.

This is the php code that I'm using. I added the name and telephone number fields.


<?php
/*
This is the e-mail address you want to send to.
*/
$webmaster_email = "editor@theeagleextra.com";

/*
These are the URLs of supporting pages.
*/
$feedback_page = "feedback_form.html";
$error_page = "error_message.html";
$thankyou_page = "thank_you.html";

/*
This loads the form field data into variables;
add additional fields here.
*/
$comments = $_REQUEST['comments'] ;
$name = $_REQUEST['name'] ;
$telephone = $_REQUEST['telephone'] ;
$email_address = $_REQUEST['email_address'] ;
/*
This checks for e-mail injection and carriage return
used by spammers to inject CC list.
*/
function isInjected($str) {
$injections = array('(\n+)',
'(\r+)',
'(\t+)',
'(%0A+)',
'(%0D+)',
'(%08+)',
'(%09+)'
);
$inject = join('|', $injections);
$inject = "/$inject/i";
if(preg_match($inject,$str)) {
return true;
}
else {
return false;
}
}

// If user tries to access php script directly, redirect to the comment form.
if (!isset($_REQUEST['email_address'])) {
header( "Location: $feedback_page" );
}

// If any fields in form are empty, redirect to the error page.
elseif (empty($comments) || empty($name) || empty($telephone) || empty($email_address)) {
header( "Location: $error_page" );
}

// If email injection is detected, go to error page.
elseif ( isInjected($email_address) ) {
header( "Location: $error_page" );
}

// Send email then redirect to the thank you page if all things met.
else {
mail( "$webmaster_email", "Feedback Form Results",
$comments, "From: $email_address" );
header( "Location: $thankyou_page" );
}
?>

Any help would be appreciated. Thanks.

--Kevin

.

Fou-Lu
01-23-2012, 04:54 PM
Its a simple matter of making what you want to show:


$body =
'Name: ' . $name .
'Email: ' . $email_address .
'Telephone Number: ' . $telephone .
PHP_EOL .
'Comments: ' . $comments;

Then sending the mail() with $body instead of $comments. $body here has to be done after those variables are created.

Kevin_M_Schafer
01-23-2012, 05:58 PM
Fou-Lu,

Thank you for the reply. I'm not altogether clear as to where to insert the "body", is this correct?


// If email injection is detected, go to error page.
elseif ( isInjected($email_address) ) {
header( "Location: $error_page" );
}

$body =
'Name: ' . $name .
'Email: ' . $email_address .
'Telephone Number: ' . $telephone .
PHP_EOL .
'Comments: ' . $comments;

// Send email then redirect to the thank you page if all things met.
else {
mail( "$webmaster_email", "Feedback Form Results",
$body, "From: $email_address" );
header( "Location: $thankyou_page" );
}
?>

I did get an error on the line that begins with "else." This is all new to me, so much of this is confusing. I do catch on quick, though, it just takes a bit to understand what I'm doing.

--Kevin

.

Fou-Lu
01-23-2012, 06:39 PM
You cannot break an if/elseif/else with expressions between. The $body can go either immediately after the creation of the variables in use, or within the else.

Kevin_M_Schafer
01-23-2012, 08:03 PM
I repositioned the body above the "If". That worked. Thank you for your help, Fou-Lu. I appreciate it.

--Kevin

.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum