Contact form question

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
pendleton21
Forum Newbie
Posts: 2
Joined: Sun Jul 11, 2010 12:53 pm

Contact form question

Post by pendleton21 »

Hello,

I'm sure this probably the easiest question you'll have ever had on this forum but can anyone tell me how I can change the actual email content that is sent out using a contact form I have put on my site? At the moment All I get in the email that is sent out when the form is submitted is the actual message. What I would ideally like in the email is something like this:

Name: John Smith
Email: john@smith.com
Phone: 1234 5678
Message:
Hello, this is my message.

Below is the contact form php code:

Code: Select all

<?php

include 'config.php';

error_reporting (E_ALL ^ E_NOTICE);

$post = (!empty($_POST)) ? true : false;

if($post)
{
include 'functions.php';

$name = stripslashes($_POST['name']);
$email = trim($_POST['email']);
$phone = stripslashes($_POST['phone']);
$subject = 'Website contact form';
$message = stripslashes($_POST['message']);

$error = '';

// Check name
if(!$name)
{
$error .= '<li>Please enter your name.</li>';
}

// Check email
if(!$email)
{
$error .= '<li>Please enter an email address.</li>';
}

if($email && !ValidateEmail($email))
{
$error .= '<li>Please enter a valid e-mail address</li>';
}

// Check message (length)
if(!$message)
{
$error .= "<li>Please enter your message</li>";
}

if(!$error)
{
$mail = mail(WEBMASTER_EMAIL, $subject, $message,
     "From: ".$name." <".$email.">\r\n"
    ."Reply-To: ".$email."\r\n"
    ."X-Mailer: PHP/" . phpversion());

if($mail)
{
echo 'OK';
}
}
else
{
echo '<div class="notification_error"><ul>'.$error.'</ul></div>';
}
}
?>
Any help would be greatly appreciated as I've never done anything with PHP before.

Thanks in advance.
User avatar
liljester
Forum Contributor
Posts: 400
Joined: Tue May 20, 2003 4:49 pm

Re: Contact form question

Post by liljester »

You could set $message so that it includes the information you want. For example:

Code: Select all

$name = stripslashes($_POST['name']);
$email = trim($_POST['email']);
$phone = stripslashes($_POST['phone']);
$subject = 'Website contact form';
$message = "Name: {$name}\nEmail: {$email}\nPhone: {$phone}\nMessage:\n". stripslashes($_POST['message']);
User avatar
morris520
Forum Commoner
Posts: 60
Joined: Thu Sep 18, 2008 8:56 pm
Location: Manchester UK

Re: Contact form question

Post by morris520 »

just be curious for liljester's message

Code: Select all

$message = "Name: {$name}\nEmail: {$email}\nPhone: {$phone}\nMessage:\n". stripslashes($_POST['message']);
What does the bracket mean outside the variable? e,g, {$name}
Could you email me if you wish to answer me?
Many thanks
User avatar
liljester
Forum Contributor
Posts: 400
Joined: Tue May 20, 2003 4:49 pm

Re: Contact form question

Post by liljester »

click the link and scroll down to "Complex (curly) syntax"
http://us.php.net/manual/en/language.types.string.php

basically it tells php that there is a variable of some sort inside the curlies, very helpful for including array names in a string

Code: Select all

$myarray[0]['zero'] = "ABC";
print"abc{$myarray[0]['zero']}123";
//  prints: abcABC123
pendleton21
Forum Newbie
Posts: 2
Joined: Sun Jul 11, 2010 12:53 pm

Re: Contact form question

Post by pendleton21 »

Hey thanks so much liljester, will give that a try when I get home from work.
jessysmithpoo
Forum Newbie
Posts: 1
Joined: Mon Jul 12, 2010 5:08 am

Re: Contact form question

Post by jessysmithpoo »

cool thanks! exactly what i was looking for.
Post Reply