Sending HTML Form with PHP

the mail() function

Moderators: egami, macek, gesf

Post Reply
New php-forum User
New php-forum User
Posts: 17
Joined: Tue Apr 19, 2011 4:23 am

Tue Apr 26, 2011 1:03 pm

I am new to PHP and would like to be able to put an enquiry form on my web site. I have written the HTML form and tested on my server by sending the input to a php results page on the same server. Can anyone tell me tell me what code I need to be ble to send the form to my email address so customers can send enquiries. I have tested my server with a simple script and I know the email will work but I don't know how to write the script to send an html form.

Here is the HTML I wrote:

<form action="result.php" method="post" class="contact">

<legend>Tell us about your event or party</legend>

<label for="firstname" class="fixedwidth">First name:</label>
<input type="text" name="firstname" id="firstname" />

<label for="lastname" class="fixedwidth">Last name:</label>
<input type="text" name="lastname" id="lastname" />

<label for="email" class="fixedwidth">Email address:</label>
<input type="text" name="email" id="email" />

<label for="telephone" class="fixedwidth">Phone number:</label>
<input type="text" name="telephone" id="telephone" />

<label for="eventname" class="fixedwidth">What kind of event is it?:</label>
<input type="text" name="eventname" id="eventname" />

<label for="eventlocation" class="fixedwidth">Where is the event? Town / area:</label>
<input type="text" name="eventlocation" id="eventlocation" />

<label for="eventdate" class="fixedwidth">When is it? Date required:</label>
<input type="text" name="eventdate" id="eventdate" />

<label for="entertainment" class="fixedwidth">Entertainment required?:</label>
<input type="text" name="entertainment" id="entertainment" />

<label for"details">Other information:</label>
<textarea id="details" name="details" cols="20" rows="7"> </textarea>

<div class="buttonarea">
<input type="submit" value="Click here to send your enquiry!" />


This was the script I wrote to handle the form :

<?php // reciving data from form on enquiry page

ini_set('display_errors',1); // Let me learn from my mistakes!

$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
$telephone = $_POST['telephone'];
$eventname = $_POST['eventname'];
$eventlocation = $_POST['eventlocation'];
$eventdate = $_POST['eventdate'];
$entertainment = $_POST['entertainment'];
$details = $_POST['details'];

// print the received data

print "<p>Thank you $firstname $lastname, for your enquiry</p>
<p>Your email address is $email and your phone number is $telephone.</p>
<p>Your event is $eventname.</p>
<p>The location is $eventlocation.</p>
<p>The date is $eventdate.</p>
<p>You require a quote for $entertainment.</p>
<p>You also told us : $details</p>
<p>Thank you for your enquiry we will get back to you soon.</p>";


I am now confused about using the mail function. Can anyone help? I've been reading up on this but can't fathom it out.

Many thanks,


User avatar
php-forum Fan User
php-forum Fan User
Posts: 979
Joined: Thu Feb 17, 2011 6:52 am
Location: Racine, WI

Tue Apr 26, 2011 4:08 pm

Mail function requires 4 inputs:

To: Who you are sending this to, in your case it will be your e-mail

Subject: The subject of the e-mail

Message: The Body of the e-mail

Headers: This contains things like BCC, CC, From, and Meta Data for the e-mail

So in your case replace:

Code: Select all

print "<p>Thank you $firstname $lastname, for your enquiry</p>
<p>Your email address is $email and your phone number is $telephone.</p>
<p>Your event is $eventname.</p>
<p>The location is $eventlocation.</p>
<p>The date is $eventdate.</p>
<p>You require a quote for $entertainment.</p>
<p>You also told us : $details</p>
<p>Thank you for your enquiry we will get back to you soon.</p>";

Code: Select all

$to = $email;//really you could just use e-mail but I am making the point about structure
$subject = "Your Enquiry to for $eventname at $eventlocation";
$message = "Thank you $firstname $lastname for your enquiry
Your e-mail address is $email and your phone number is $telephone.
Your event is $eventname.
The location is $eventlocation.
The date is $eventdate.
You require a quote for $entertainment.
You also told us: $details
Thank you again for your enquiry, we will get back to you soon";

$headers = 'From:' . "\r\n" .
    'Reply-To:' . "\r\n" 

//now you have defined all of the arguments for the mail function all that is left is to call it
Lastly, as a side note, If you are planning on submitting the variables from the form you should sanitize them. The easiest way to do this is using the trim() and addslashes() functions on any input. These remove white space and escape any characters that will cause your query to fail.

New php-forum User
New php-forum User
Posts: 17
Joined: Tue Apr 19, 2011 4:23 am

Tue Apr 26, 2011 9:16 pm

Thanks, great stuff. I've just had a go at substituting the code but it's not working. I'm a bit thick with these things, I've obviously done something wrong. It's 6am here in the UK so I've got to get ready and go to work. I'll have another go at this tonight. I deleted everything from PRINT ( left print in the script ) and copied in the new code. When I tested the form it wouldn't go to the results page I just got a blank screen and no error message. I'll try again when I get home, cheers, Ron.

Post Reply