Php contact form question


Trying to get my php form to send me the info from the completed fields. I’m using a form which I found here and you can see all the code.

Form works fine, but problem I am having is when I try to add new fields, for example:

$customer_name = $_REQUEST['customer_name'] ;

I would think I would need to add the $customer_name value in the following code along with the $comments that was already there, so I receive that info, but when I test the form it does not work - do not receive an email at all.

// If we passed all previous tests, send the email then redirect to the thank you page.
else {
mail( "$webmaster_email", "Feedback Form Results",
  [COLOR="Red"]$customer_name[/COLOR], $comments, "From: $email_address" );
header( "Location: $thankyou_page" );

Any idea what’s going on?
Thanks :slight_smile:


change to:

mail( "$webmaster_email", "Feedback Form Results",
  $customer_name. ' said: ' . $comments, "From: $email_address" );
header( "Location: $thankyou_page" );

although it sounds as if something else is doing some cleansing (lets hope so ;))

If we passed all previous tests

Looking at the PHP: Mail - Manual, it should be:

bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )

So in your code it’s:

mail( "$webmaster_email", // To - you don't need the " in this line as it just slows down execution times.
"Feedback Form Results", // Subject
 $customer_name,  // Message
$comments, // Additional Headers - I doubt $comments is header data
"From: $email_address" // Additional Parameters // this is also not an additional parameter.

Your probably better off with something like this:

// $customer_name = $_REQUEST['customer_name'] ; // I'm not a fan of _REQUEST, I prefer $_POST it's more specific. 
$customer_name = $_POST['customer_name'];

// Run tests.

$to  = $webmaster_email;
$subject = 'Feedback Form Result'; // Also single quotes are faster then double quotes.
$message = $customer_name."\\r\
"From: ".$email_address; // \\r\
 creates a new line.

$headers = 'From:' . "\\r\
" . // These extra headers reduce the odds it will end up in the spam bin.
    'Reply-To:' . "\\r\
" .
    'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);

header( 'Location: '.$thankyou_page );
die(); // it's a good idea to kill the script once you send a location header as some browsers disregard them.

Thanks Cups & Rogem. I am receiving all the values now. However, I am now trying to get my checkbox results displayed. I am able to get one selection the user chose to display, but if multiple checkbox items are selected, only displays one item, not all.

I’ve been researching and playing around with some code, but can’t get to work with my php send mail.

Thinking I need to do something like this ??

$N = count($aDoor);

echo("You selected $N door(s): ");
for($i=0; $i < $N; $i++)
  echo($aDoor[$i] . " ");


and my html page would look like this:

<input type=“checkbox” name=“formDoor” value=“A” />Acorn Building<br />
<input type=“checkbox” name=“formDoor” value=“B” />Brown Hall<br />

I’m not much of a programmer…:confused: Maybe there’s an easier way. Doesn’t have to be pretty, just need to see all checkbox items selected.

Try this:

<input type="checkbox" name="formDoor[0]" value="A" />Acorn Building<br />
<input type="checkbox" name="formDoor[1]" value="B" />Brown Hall<br />

Then alter it and try this

// NB do not quote array keys in your html, even non-numeric !!

<input type="checkbox" name="formDoor[A]" />Acorn Building<br />
<input type="checkbox" name="formDoor[B]" />Brown Hall<br />

Just use

var_dump($_POST); // or $_GET

on the postback to see what is being passed around between html and php

Hi I’m struggling with making an application form for an account on a site, information i want to receive is the applicants first name, last name, email, and a brief description. When I have had a go at writing the php code to email me the info, it sends an error message saying that ‘we encountered an error’…???

When starting out, strip everything right down to the bare bones. Forget sending emails, add that later when you are confident that your form is being handled. Strip out styling too. Keep it really simple, and slowly build it up.

Make a simple form, say using the POST method, when the submit button is hit, just display the output of


and check that against your expectations. Then fiddle with the form again, and repeat.

Well I seem to have screwed something up that I need to fix before I worry about the checkbox selections.

  1. In my php mail script, email address and comment is currently defined as mandatory fields as is noted in the script. However, for some reason it is making phone_number and comments as mandatory when I test the form.
// If the form fields are empty, redirect to the error page.
elseif (empty($email_address) || empty($comments)) {
header( "Location: $error_page" );

I’ve defined my fields as follows, and this is the order they appear on the form itself and are defined by the same name.

$customer_name = $_REQUEST['customer_name'] ;
$email_address = $_REQUEST['email_address'] ;
$phone_number = $_REQUEST['phone_number'] ;
$comments = $_REQUEST['comments'] ;

  1. Finally, for the following, when I receive the email it is not displaying the customer name as the From - is just blank.
// If we passed all previous tests, send the email then redirect to the thank you page.
else {
mail( "$webmaster_email", "Feedback Form Results",
  $customer_name. ' said: ' . $comments, "From: $customer_name" );
header( "Location: $thankyou_page" );

Any assistance greatly appreciated…I’m going crazy:sick: