Jump to content

Emailing Form Data using PHP Script


ben_1uk

Recommended Posts

Hi everyone,

 

I have created a HTML form for collecting data on a web page, ie, name, email address, etc, which works fine. I have then asked for that data to be emailed to me using the POST action in conjunction with a PHP script called "carparkprocess2.php".

 

At the moment, the form works OK on the web page, the confirmation message to say the data has been submitted successfully works OK, but the email that should display the information entered into the form doesn't work properly. It comes through and is formatted OK, but the form information is missing.

 

I've been trying to resolve this issue for 2 days straight now and have tried loads of different things! Can anybody help? I need a quick answer on this one if possible  ;)

 

<?php

error_reporting(E_ALL ^ E_NOTICE); // Prints all errors except Notices.

/* Subject and Email Variables */

$emailSubject = 'Car Park Reservation Confirmation';
$webMaster = '[email protected]';

/* Gathering Data Variables */

if (!isset($_POST['name']))
{
$_POST['name'] = "name";
} 
if (!isset($_POST['carregistration']))
{
$_POST['carregistration'] = "carregistration";
}
if (!isset($_POST['emailaddress']))
{
$_POST['emailaddress'] = "emailaddress";
}
if (!isset($_POST['numberinparty']))
{
$_POST['numberinparty'] = "numberinparty";
}

$body = <<<EOD
<br><hr><br>
Name: $name <br>
Vehicle Registration: $carregistration <br>
Email Address: $emailaddress <br>
Number in Party: $numberinparty <br>
EOD;

$mailheaders .= "From: $emailaddress\r\n";
$headers .= "Content-type: text/html";
$success = mail($webMaster, $emailSubject, $body, $headers);

/* Results rendered as HTML */

$theResults = <<<EOD
<html>
<head>
<title>Car Park Reservation Confirmation</title>
<meta http-equiv='Content-Type' content='text/html'>
<style type='text/css'>
<!--
body { 
min-height: 100%; height: auto; background: #000000; color: #ffffff; font-size: 14px; font-family: Verdana, Arial, Helvetica, sans-serif; margin: 0; 
}
-->
</style>
</head>

<div>
<div align='left'>Your car park reservation request has been successful</div>
</div>
</body>
</html>
EOD;
echo "$theResults";

?>

 

Here is the HTML within the form:

 

<form method='post' action='carparkprocess2.php'>

Link to comment
https://forums.phpfreaks.com/topic/228080-emailing-form-data-using-php-script/
Share on other sites

To me you are switching the assignments, i.e.

if (!isset($_POST['name']))
   {
   $_POST['name'] = "name";
   } 

should be

if (!isset($_POST['name']))
   {
   $name = $_POST['name'];
   } 

this goes for all the assignments from the $_POST array.

Thanks for the reply.

 

I have now got rid of the "isset" command as I had only included this originally to try and resolve the "undefined variable" warning messages. However, I have since included the below code to stop this from happening:

 

error_reporting(E_ALL ^ E_NOTICE); // Prints all errors except Notices.

 

Not the "correct" fix I know, but I was going round in circles trying to resolve something that strictly speaking isn't an error.

 

I have simply changed:

 

/* Gathering Data Variables */

   if (!isset($_POST['name']))
   {
   $_POST['name'] = "name";
   } 
   if (!isset($_POST['carregistration']))
   {
   $_POST['carregistration'] = "carregistration";
   }
   if (!isset($_POST['emailaddress']))
   {
   $_POST['emailaddress'] = "emailaddress";
   }
   if (!isset($_POST['numberinparty']))
   {
   $_POST['numberinparty'] = "numberinparty";
   }

 

to...

 

/* Gathering Data Variables */

$name = $_POST['name'];
$carregistration = $_POST['carregistration'];
$emailaddress = $_POST['emailaddress'];
$numberinparty = $_POST['numberinparty'];

 

...and it now works  8) I just need to add in some validation now.

 

Thanks,

 

BB2011

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.