...

View Full Version : problem of showing only the filled in results sent to email



Razzy
08-03-2012, 06:54 PM
hey people, is there any way of sending only the filled in details to email?

i will have roughly 60 input fields in a order form, however all 60 input fields are not mandatory.

the coding so far is:




<?php
if (isset($_REQUEST['email']))
//if "email" is filled out, send email

{


//send email
$to = "razzyc86@hotmail.co.uk";
$subject = "order form submitted";
$email = $_REQUEST['email'] ;

// To allow HTML codes to email!
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: $email\r\n";


$message = "
<html>
<head><title>Order placed</title></head>
<body>
<p>Email is from ".$email."</p>
<p>First name: ".$_POST["first_name"]."<br />
Last name: ".$_POST["last_name"]."<br />
Address: ".$_POST["address"]."<br/>
Postcode: ".$_POST["postcode"]."<br />
Tel no: ".$_POST["tel_no"]."<br />
</p>
</body>
</html>";

mail($to,$subject,$message,$headers);

echo "Thank you for using our mail form";
}

?>



so basically so far i have 5 fields:

first name: xxxxxxx
last name: xxxxxxxxxx
address: xxxxxxxxxxxxxxx
postcode:xxxxxxxx
tel no: xxxxxxxxxxxx

customer does not need to enter the postcode, so if they leave that blank then i dnt want the postcode part to show up on the email body, for example:

first name: xxxxxxx
last name: xxxxxxxxxx
address: xxxxxxxxxxxxxxx
tel no: xxxxxxxxxxxx

i hope i dont need to make drastic changes to the coding so far as some of the guys here have helped me alot to create the php email coding

thank you very much :)

mlseim
08-03-2012, 07:49 PM
$message = "
<html>
<head><title>Order placed</title></head>
<body>
<p>Email is from ".$email."</p>
<p>First name: ".$_POST["first_name"]."<br />
Last name: ".$_POST["last_name"]."<br />
Address: ".$_POST["address"]."<br/>";

if($_POST['postcode']){
$message .= "
Postcode: ".$_POST["postcode"]."<br />";
}

$message .= "
Tel no: ".$_POST["tel_no"]."<br />
</p>
</body>
</html>";

ECoode
08-03-2012, 07:54 PM
$message = "
<html>
<head><title>Order placed</title></head>
<body>
<p>Email is from ".$email."</p>
<p>First name: ".$_POST["first_name"]."<br />
Last name: ".$_POST["last_name"]."<br />
Address: ".$_POST["address"]."<br/>";

if($_POST['postcode']){
$message .= "
Postcode: ".$_POST["postcode"]."<br />";
}

$message .= "
Tel no: ".$_POST["tel_no"]."<br />
</p>
</body>
</html>";


Shouldn't it be:


if( empty($_POST['postcode']) ){

Or it doesn't matter?

AndrewGSW
08-03-2012, 08:12 PM
Here is my attempt but I've not had a chance to test it.


$message = "
<html>
<head><title>Order placed</title></head>
<body>
<p>Email is from ".$email."</p>
<p>";

$details = array(array("First name" => "first_name"), /* repeat.. */ );
$trimmed = array_map('trim', $_POST);
$cleaned = array_map('htmlentities', $trimmed);

foreach ($details as $fields) {
foreach ($fields as $title => $fieldname) {
if (isset($cleaned["'$fieldname'"]) && !empty($cleaned["'$fieldname'"])) {
$message .= "$title: {$cleaned['$fieldname']}</br>";
}
}
}

$message .= "</p>
</body>
</html>";

Not sure at the moment if this: isset($cleaned["'$fieldname'"]) - is correct(?).

It seems to be a common omission for people not to clean post data before re-inserting it into html.

@ECoode I prefer to explicitly use isset() and empty() but I believe 'if($_POST['postcode'])' is equivalent.

Razzy
08-03-2012, 08:15 PM
thanx guys, however i only used the postcode field as an example, majority of the 60+ fields will not be mandatory, does that mean im gonna have to use the....if($_POST[...code on each of the non mandatory fields? or is there a simpler way of using one code for all the fields>

thanx

AndrewGSW
08-03-2012, 08:24 PM
Following my previous post, if the fields are consistently named like this: 'first_name', 'address' you could loop through them converting them into a new array with 'First name', 'first_name', 'Address', 'address', etc.

Using this approach you wouldn't have to type all 60 title/field name combinations. You would just need to exclude those fields that don't need to be checked, and probably manually insert 'Post Code', 'postcode'.

AndrewGSW
08-03-2012, 08:42 PM
Something like this:


<?php
$message = "
<html>
<head><title>Order placed</title></head>
<body>
<p>Email is from ".$email."</p>
<p>";

// $details = array(array("First name" => "first_name"), /* repeat.. */ );
$trimmed = array_map('trim', $_POST);
$cleaned = array_map('htmlentities', $trimmed);

$extracted = array();
$exclude = array("field_name1", "fieldname2"); // the fields you don't need to check

foreach ($cleaned as $key => $value) {
if (!in_array($key, $exclude)) {
$extracted[] = array(ucwords(str_replace("_", " ", $key)), $value);
}
}

foreach ($extracted as $fields) {
if (!empty($fields[1])) {
$message .= "$fields[0]: $fields[1]</br>";
}
}

$message .= "</p>
</body>
</html>";

?>

AndrewGSW
08-03-2012, 09:04 PM
If you want to do it in one loop..


<?php
$message = "
<html>
<head><title>Order placed</title></head>
<body>
<p>Email is from ".$email."</p>
<p>";

$trimmed = array_map('trim', $_POST);
$cleaned = array_map('htmlentities', $trimmed);

$exclude = array("field_name1", "fieldname2"); // the fields you don't need to check

foreach ($cleaned as $key => $value) {
if (!in_array($key, $exclude) && !empty($value)) {
$message .= ucwords(str_replace("_", " ", $key)) . ": $value</br>";
}
}

$message .= "</p>
</body>
</html>";

?>

Razzy
08-03-2012, 11:00 PM
ahh thank you andrewGSW, i decided to do it in one loop and its perfect :)

AndrewGSW
08-03-2012, 11:16 PM
ahh thank you andrewGSW, i decided to do it in one loop and its perfect :)

Cool, no problem. Thank you for testing it for me :thumbsup:

DrDOS
08-03-2012, 11:31 PM
So you have five or so required fields, you can give them all individual names. But of the fifty five or so left, what kind of data will they have? Are they just a list of different things? You could give them all the same name like this name="order[]", and when you upload they would be an array, you could just loop over the elements to get the value, put an if statement in to remove any empty elements, and that would do it. You could even have javascript make as many inputs as you needed.

AndrewGSW
08-04-2012, 02:53 AM
So you have five or so required fields, you can give them all individual names. But of the fifty five or so left, what kind of data will they have? Are they just a list of different things? You could give them all the same name like this name="order[]", and when you upload they would be an array, you could just loop over the elements to get the value, put an if statement in to remove any empty elements, and that would do it. You could even have javascript make as many inputs as you needed.

The inputs need to keep their individual names as these are converted to headings within the email.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum