Very basic help needed with mail form

Hello! I’m using a free “contact” form that worked just fine until I added more fields and then tried to get them to be included in the email that’s sent. Now I don’t get any errors but the email isn’t being sent.

I know absolutely nothing about PHP. I did try to find the answer prior to posting here but I just don’t know enough to figure it out myself.

The form initially had just two fields - email address and message. I added several additional fields but they weren’t included in the email. I don’t know the proper format for adding them so they’ll appear in the body of the message.

Here’s the relevant code:
$first_name = $_REQUEST[‘first_name’] ;
$last_name = $_REQUEST[‘last_name’] ;
$email_address = $_REQUEST[‘email_address’] ; (this was an original field)
$phone = $_REQUEST[‘phone’] ;
$country = $_REQUEST[‘country’] ;
$adults = $_REQUEST[‘adults’] ;
$children = $_REQUEST[‘children’] ;
$arrival = $_REQUEST[‘arrival’] ;
$departure = $_REQUEST[‘departure’] ;
$comments = $_REQUEST[‘comments’] ; (the other original field)

(Original, working code):
mail( “$webmaster_email”, “This is the subject line”,
$comments, “From: $email_address” );
header( “Location: $thankyou_page” );

(With my additional variables):
mail( “$webmaster_email”, “This is the subject line”,
$first_name, $last_name, $phone, $country, $adults, $children, $arrival, $departure, $comments, “From: $email_address” );
header( “Location: $thankyou_page” );

Here is How It would work. You need to have some extra validation on the fields before sending the email, but anyways I’ve corrected your code with the variables you want, no extra validation is done otherwise it may complicate things for you. :slight_smile:

[php]<?php

if ($_SERVER[‘REQUEST_METHOD’] == ‘POST’) {

/* User supplied information */
	$first_name = $_POST['first_name'];
	$last_name = $_POST['last_name'];
	$email_address = $_POST['email_address'];
	$phone = $_POST['phone'];
	$country = $_POST['country'];
	$adults = $_POST['adults'];
	$children = $_POST['children'];
	$arrival = $_POST['arrival'];
	$departure = $_POST['departure'];
	$comments = $_POST['comments']; 

/* Site Administrators's Information */
	$to = '[email protected]';
	$subject = 'Your Email Subject Line';

/* You can customize this email message to your own needs. You can also use HTML elements and user supplied inforamtion in here too */

	$message = "A new user has treid to contact you via Contact Form. His / Her details are as follows: <br />
				First Name: {$first_name} <br />
				Last Name: {$last_name} <br />
				Email Address: {$email_address} <br />
				Phone Number: {$phone} <br />
				Country: {$country} <br />
				Adults: {$adults} <br />
				Children: {$children} <br />
				Arrival: {$arrival} <br />
				Departure: {$departure} <br />
				Comments: {$comments} <br />

				";
    if (mail($to, $subject, $message){

    	header('Location: Your_thank_you_page_here.php');

    } else {

    	header('Location: email_not_sent_error_page.php');

    }

?>[/php]

I would not use the $_POST method… I would keep the original $_REQUEST. Reason being it responds to both POST and GET. Also I am not sure why the previous poster put in an opening if statement that is never closed… I would just remove that first if line all together.

Thanks for pointing out my mistake I forgot the closing curly brace for IF block. :’(
The reason I changed $_REQUEST to $_POST is because user is submitting information not fetching.

@Gazra add a closing curly brace just before the closing php delimiter i.e. ?> so it looks something like this.

[php] }
?>[/php]

You should ways use post or get, never request. Request can have very unexpected results since it includes both post and get vars at the same time. Depending on the settings in the ini file and how you are validating the requested items, you can unexpectedly create vars that have been specified by the client that should not exist.

You will only have variable collision you are referring to if you are not following good variable naming standards. If you are using the same variable names to pass POST, GET and COOKIE data around you have bigger issues than using $_REQUEST vs $_POST or $_GET.

Sponsor our Newsletter | Privacy Policy | Terms of Service