Trouble with contact form - adding variables

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Forum Newbie
Posts: 1
Joined: Tue Feb 05, 2013 4:35 pm

Trouble with contact form - adding variables

Post by nickburns480 »

I can not for the life of me figure out how to send all the variables in the email message. Here is my code:

//client variables
$name = $_POST["name"];
$title = $_POST["title"];
$ftlt = $_POST["ftlt"];
$phone = $_POST["subject"];
$mobile = $_POST["mobile"];
$street = $_POST["street"];
$city = $_POST["city"];
$zip = $_POST["zip"];
$license = $_POST["license"];
$desired = $_POST["desired"];
$subject = $_POST["subject"];
$message = $_POST["message"];
$email = $_POST["email"];
$verif_box = $_POST["verif_box"];

$message = stripslashes($message);
$subject = stripslashes($subject);
$name = stripslashes($name);
$title = stripslashes($title);
$ftlt = stripslashes($mobile);
$phone = stripslashes($phone);
$mobile = stripslashes($mobile);
$street = stripslashes($state);
$city = stripslashes($city);
$zip = stripslashes($zip);
$license = stripslashes($license);
$desired = stripslashes($desired);

// validate verification code
if(md5($verif_box).'a4xn' == $_COOKIE['tntcon']){
// if pass send message
mail("", 'Online Form: '.$subject, $_SERVER['REMOTE_ADDR']."\n\n".$message, "email: $email");
// cookie deletion
} else if(isset($message) and $message!=""){
// if verif entry incorrect error message
header("Location: index.php?subject=$subject&from=$from&message=".urlencode($message)."&wrong_code=true");
} else {
echo "no variables received, this page cannot be accessed directly";

His issue is with this line of code. How do we add the client variables to achieve proper output in the sent message?

mail("", 'Online Form: '.$subject, $_SERVER['REMOTE_ADDR']."\n\n".$message, "email: $email");

Would I use the "\n\nname: ".$name. for each variable?

My php coding is minimal. Any help appreciated!
User avatar
Forum Contributor
Posts: 375
Joined: Wed Apr 14, 2010 4:45 pm
Location: UK

Re: Trouble with contact form - adding variables

Post by mecha_godzilla »


You need to concatenate all the form values - as an example:

Code: Select all

$message .= 'Sender: ' . $name . PHP_EOL;
The important point to look at in this code is the .= operator, because what this does is append any new information to that already stored in the $message variable. The PHP_EOL code is used when you're outputting the email in plaintext format (rather than HTML) to create a compatible line break - if you *are* using HTML then you can either format the different sections of the message into their own paragraphs or just add line breaks (<br> for HTML 4.1).


Mecha Godzilla
Post Reply