Hey guys,
I need some suggestions, I'm kinda lost on how to go about this.
When a customer fills out a form and hits submit an email is generated using the mail() function and is sent to me with the info they provided. There is a lot of options on the form and not all of them are required so I want to loop through the form varibles and only pull out the ones that the customer has opted to fill out. That way I can only populate the email with only the form elements that the customer has filled out.
Here is the form to get an idea: http://www.phoenixmason.com/estimate.php
Any suggestions on how to go about this?
Thanks.
Suggestions...?
Moderator: General Moderators
Maybe something with a foreach loop:
However, you'll have to test this first and maybe some others have better suggestions. This is just a quick example.
Code: Select all
<?php
$message = '';
foreach ($_POST as $key => $value)
{
if(!empty($_POST[$key]) && strlen($_POST[$key]) > 0)
{
$message .= $value;
}
}
?>- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
You might want to explain if/why matthijs' suggestion doesn't work for you.
same basic thing matthijs already suggested:
array_filter($_POST,'strlen')
then foreach like normal.
same basic thing matthijs already suggested:
array_filter($_POST,'strlen')
then foreach like normal.
Matthijs suggestion is working but its not exactly what I am looking for. Let me show you.
Like I said I want to populate the email with only the form elements that have been filled out or selected. You can look at the form here: http://www.phoenixmason.com/estimate.php. All what I really care about is the Patios, Walkways, etc. section. If they only choose one, like flagstone patio then it works fine, but if they choose a patio and a walkway then thats where my problem begins.
Here is my code:
My output is repeating both the patios and walkways if they choose both.
Output:
Patio: Paver ,Stamped Concrete ,Walkway: Paver ,Stamped Concrete
It should read:
Patio: Paver
Walkway: Stamped Concrete
If that makes any sense at all the help is very appreciated.
Thanks
Like I said I want to populate the email with only the form elements that have been filled out or selected. You can look at the form here: http://www.phoenixmason.com/estimate.php. All what I really care about is the Patios, Walkways, etc. section. If they only choose one, like flagstone patio then it works fine, but if they choose a patio and a walkway then thats where my problem begins.
Here is my code:
Code: Select all
<?php
//PATIO FORMATING---------------------------------------------
$ppaver = $_POST['ppaver'];
$pbrick = $_POST['pbrick'];
$pflagstone = $_POST['pflagstone'];
$pcrete = $_POST['pcrete'];
$pstamped = $_POST['pstamped'];
$pnotsure = $_POST['pnotsure'];
$pmsg = '';
foreach ($_POST as $key => $value)
{
if(!empty($_POST[$key]) && strlen($_POST[$key]) > 0)
{
$pmsg .= $value . ' ,';
}
}
if ($pmsg > '')
{
$pmsgnew = 'Patio: ' . $pmsg;
}
//WALKWAY FORMATING--------------------------------------------
$wbrick = $_POST['wbrick'];
$wflagstone = $_POST['wflagstone'];
$wstamped = $_POST['wstamped'];
$wnotsure = $_POST['wnotsure'];
$wmsg = '';
foreach ($_POST as $key => $value)
{
if(!empty($_POST[$key]) && strlen($_POST[$key]) > 0)
{
$wmsg .= $value . ' ,';
}
}
if ($pmsg > '')
{
$wmsgnew = 'Walkway: ' . $wmsg;
}
?>Output:
Patio: Paver ,Stamped Concrete ,Walkway: Paver ,Stamped Concrete
It should read:
Patio: Paver
Walkway: Stamped Concrete
If that makes any sense at all the help is very appreciated.
Thanks
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
How about
If your field names aren't as readable as you like, you have create a map to readable names:
Code: Select all
$msg = '';
foreach ($_POST as $key => $value)
{
if(!empty($_POST[$key]) && strlen($_POST[$key]) > 0)
{
$msg .= $key . ': ' .$value . "\n";
}
}Code: Select all
$map = array('field1' => 'Paver', 'field2' => 'Walkway');
$msg = '';
foreach ($_POST as $key => $value)
{
if(!empty($_POST[$key]) && strlen($_POST[$key]) > 0)
{
if(isset($map[$key]))
{
$key = $map[$key];
}
else
{
$key = 'Unknown field (' . $key . ')';
}
$msg .= $key . ': ' .$value . "\n";
}
}