Suggestions...?

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
mattyboi
Forum Commoner
Posts: 34
Joined: Mon Feb 06, 2006 9:42 pm

Suggestions...?

Post by mattyboi »

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.
matthijs
DevNet Master
Posts: 3360
Joined: Thu Oct 06, 2005 3:57 pm

Post by matthijs »

Maybe something with a foreach loop:

Code: Select all

<?php
$message = '';

foreach ($_POST as $key => $value)
{
   if(!empty($_POST[$key]) && strlen($_POST[$key]) > 0)
   {
        $message .= $value;
    }
}
?>
However, you'll have to test this first and maybe some others have better suggestions. This is just a quick example.
mattyboi
Forum Commoner
Posts: 34
Joined: Mon Feb 06, 2006 9:42 pm

Post by mattyboi »

Anyone have any other suggestions?
:?
Thanks.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

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.
mattyboi
Forum Commoner
Posts: 34
Joined: Mon Feb 06, 2006 9:42 pm

Post by mattyboi »

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:

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;
}
?>
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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

How about

Code: Select all

$msg = '';
foreach ($_POST as $key => $value)
{
   if(!empty($_POST[$key]) && strlen($_POST[$key]) > 0)
   {
        $msg .= $key . ': ' .$value . "\n";
    }
}
If your field names aren't as readable as you like, you have create a map to readable names:

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";
    }
}
Post Reply