0

Hey guys, I've been working on this for like 2 days now and I know it's something pretty simple to do, I'm just a rookie with php lol. Basically I have checkbox forms that I want to email all the checked values in.

Here is my html:

<table class="pstable">
		<tr>
			<td>Lot #</td>
			<td>Plan</td>
			<td>Yds</td>
			<td>Scaffold U</td>
			<td>Lath</td>
			<td>Stone</td>
			<td>Scratch</td>
			<td>Brown</td>
			<td>Foam</td>
			<td>Finish</td>
			<td>Prep</td>
			<td>Scaffold D</td>
			<td>Cleanup</td>
		</tr>
		<tr>
			<td><label><input type="checkbox" name="lot[]" value="3" />3</label></td>
			<td><label><input type="checkbox" name="plan[]" value="3B" />3B</label></td>
			<td><label><input type="checkbox" name="yards[]" value="415" />415</label></td>
			<td><label><input type="checkbox" name="scaffoldU[]" value="456" />456</label></td>
			<td><label><input type="checkbox" name="lath[]" value="1,182.75" />$1,182.75</label></td>
			<td><label><input type="checkbox" name="stone[]" value="560.00" />$560.00</label></td>
			<td><label><input type="checkbox" name="scratch[]" value="498.00" />$498.00</label></td>
			<td><label><input type="checkbox" name="brown[]" value="747.00" />$747.00</label></td>
			<td><label><input type="checkbox" name="foam[]" value="633.00" />$633.00</label></td>
			<td><label><input type="checkbox" name="finish[]" value="622.50" />$622.50</label></td>
			<td><label><input type="checkbox" name="prep[]" value="150.00" />$150.00</label></td>
			<td><label><input type="checkbox" name="scaffoldD[]" value="250.00" />$250.00</label></td>
			<td><label><input type="checkbox" name="cleanup[]" value="150.00" />$150.00</label></td>
		</tr>
		<tr>
			<td><label><input type="checkbox" name="lot[]" value="12" />12</label></td>
			<td><label><input type="checkbox" name="plan[]" value="1A" />1A</label></td>
			<td><label><input type="checkbox" name="yards[]" value="435" />435</label></td>
			<td><label><input type="checkbox" name="scaffoldU[]" value="479" />479</label></td>
			<td><label><input type="checkbox" name="lath[]" value="1,239.75" />$1,239.75</label></td>
			<td><label><input type="checkbox" name="stone[]" value="" /> - </label></td>
			<td><label><input type="checkbox" name="scratch[]" value="522.00" />$522.00</label></td>
			<td><label><input type="checkbox" name="brown[]" value="783.00" />$783.00</label></td>
			<td><label><input type="checkbox" name="foam[]" value="543.00" />$543.00</label></td>
			<td><label><input type="checkbox" name="finish[]" value="652.50" />$652.50</label></td>
			<td><label><input type="checkbox" name="prep[]" value="150.00" />$150.00</label></td>
			<td><label><input type="checkbox" name="scaffoldD[]" value="261.00" />$261.00</label></td>
			<td><label><input type="checkbox" name="cleanup[]" value="150.00" />$150.00</label></td>
		</tr>
		<tr>
			<td><label><input type="checkbox" name="lot[]" value="16" />16</label></td>
			<td><label><input type="checkbox" name="plan[]" value="2A" />2A</label></td>
			<td><label><input type="checkbox" name="yards[]" value="445" />445</label></td>
			<td><label><input type="checkbox" name="scaffoldU[]" value="490" />490</label></td>
			<td><label><input type="checkbox" name="lath[]" value="1,268.25" />$1,268.25</label></td>
			<td><label><input type="checkbox" name="stone[]" value="" /> - </label></td>
			<td><label><input type="checkbox" name="scratch[]" value="534.00" />$534.00</label></td>
			<td><label><input type="checkbox" name="brown[]" value="801.00" />$801.00</label></td>
			<td><label><input type="checkbox" name="foam[]" value="568.00" />$568.00</label></td>
			<td><label><input type="checkbox" name="finish[]" value="667.50" />$667.50</label></td>
			<td><label><input type="checkbox" name="prep[]" value="150.00" />$150.00</label></td>
			<td><label><input type="checkbox" name="scaffoldD[]" value="267.00" />$267.00</label></td>
			<td><label><input type="checkbox" name="cleanup[]" value="150.00" />$150.00</label></td>
		</tr>
		<tr>
			<td><label><input type="checkbox" name="lot[]" value="15" />15</label></td>
			<td><label><input type="checkbox" name="plan[]" value="3C" />3C</label></td>
			<td><label><input type="checkbox" name="yards[]" value="395" />395</label></td>
			<td><label><input type="checkbox" name="scaffoldU[]" value="435" />435</label></td>
			<td><label><input type="checkbox" name="lath[]" value="1,125.75" />$1,125.75</label></td>
			<td><label><input type="checkbox" name="stone[]" value="1225.00" />$1225.00</label></td>
			<td><label><input type="checkbox" name="scratch[]" value="474.00" />$474.00</label></td>
			<td><label><input type="checkbox" name="brown[]" value="711.00" />$711.00</label></td>
			<td><label><input type="checkbox" name="foam[]" value="660.00" />$660.00</label></td>
			<td><label><input type="checkbox" name="finish[]" value="592.50" />$592.50</label></td>
			<td><label><input type="checkbox" name="prep[]" value="150.00" />$150.00</label></td>
			<td><label><input type="checkbox" name="scaffoldD[]" value="237.00" />$237.00</label></td>
			<td><label><input type="checkbox" name="cleanup[]" value="150.00" />$150.00</label></td>
		</tr>
	</table>

Here is the PHP:

$lotnumbers = $_REQUEST['lots'];
	$lathcosts = $_REQUEST['lath'];
	$stonecosts = $_REQUEST['stone'];
	$scratchcosts = $_REQUEST['scratch'];
	$browncosts = $_REQUEST['brown'];
	$foamcosts = $_REQUEST['foam'];
	$finishcosts = $_REQUEST['finish'];
	$prepcosts = $_REQUEST['prep'];
	$scaffoldDcosts = $_REQUEST['scaffoldD'];
	$cleanupcosts = $_REQUEST['cleanup'];
	
	#foreach ($_POST['lots'] as $lotnumber) {
	#	$lotnumbers .= '' . $lotnumber . '<br />';
	#}
	#
	#foreach ($_POST['lath'] as $lathcost) {
	#	$lathcosts .= '' . $lathcost . '<br />';
	#}
	#
	#foreach ($_POST['stone'] as $stonecost) {
	#	$stonecosts .= '' . $stonecost . '<br />';
	#}
	#
	#foreach ($_POST['scratch'] as $scratchcost) {
	#	$scratchcosts .= '' . $scratchcost . '<br />';
	#}
	#
	#foreach ($_POST['brown'] as $browncost) {
	#	$browncosts .= "$browncost";
	#}
	#
	#foreach ($_POST['foam'] as $foamcost) {
	#	$foamcosts .= "$foamcost";
	#}
	#
	#foreach ($_POST['finish'] as $finishcost) {
	#	$finishcosts .= "$finishcost";
	#}
	#
	#foreach ($_POST['prep'] as $prepcost) {
	#	$prepcosts .= "$prepcost";
	#}
	#
	#foreach ($_POST['scaffoldD'] as $scaffoldDcost) {
	#	$scaffoldDcosts .= "$scaffoldDcost";
	#}
	#
	#foreach ($_POST['cleanup'] as $cleanupcost) {
	#	$cleanupcosts .= "$cleanupcost";
	#}
	
	$table_output = '
	<table>
		<tr>
			<td>Lot # -</td>
			<td> Lath -</td>
			<td> Stone -</td>
			<td> Scratch -</td>
			<td> Brown -</td>
			<td> Foam -</td>
			<td> Finish -</td>
			<td> Prep -</td>
			<td> Scaffold D -</td>
			<td> Cleanup</td>
		</tr>
		<tr>
			<td>' . $lotnumbers . '</td>
			<td>' . $lathcosts . '</td>
			<td>' . $stonecosts . '</td>
			<td>' . $scratchcosts . '</td>
			<td>' . $browncosts . '</td>
			<td>' . $foamcosts . '</td>
			<td>' . $finishcosts . '</td>
			<td>' . $prepcosts . '</td>
			<td>' . $scaffoldDcosts . '</td>
			<td>' . $cleanupcosts . '</td>
		</tr>
	</table>
	';

The foreach loops are commented out because I kept getting errors once I put more than one in the script.

Basically I want the email to display the table that was on the webpage, except with only the checked values, the rest of the spaces will be blank.

2
Contributors
6
Replies
7
Views
6 Years
Discussion Span
Last Post by ddymacek
0

can you post your whole form, are you submitting to a separate php file or is that code on the same page? you should use $_GET or $_POST to retrieve the values and probably not $_REQUEST your foreach statements were 'blowing up' because you did not check if there was data was set in the array before trying to process it. you should validate the data prior to trying to work with it.

0

I had $_POST and it didn't work, request was in there as an experiment. The form is tricky to post because there's other sections in it that don't have to do with specific area. It's submitting to an separate php file.

How do I check if there is data in the array first? When I have a single foreach statement it always works fine, no matter what array it's for. When more than 1 is active it get's all these errors that when I googled it came up saying that I forgot a ';' or '.' etc but I can't seem to find it.

0

to check you can do a couple of things, isset, count, or empty something like this:

$lotnumbers = $_REQUEST['lots'];
if (!empty($lotnumbers)) && count($lotnumbers > 0) {
    foreach($lotnubers as $number) {
        echo $number;
    }
}

try this, and restate exactly what you want your result to be I can't quite wrap my head around what you want your output to be... give me a few minutes I might have an answer for you.

0

my code was in error before, this is tested at least:

$lotnumbers = $_REQUEST['lot'];
if (!empty($lotnumbers) && count($lotnumbers) > 0) {
	foreach($lotnumbers as $number) {
        	echo $number . "<br />";
	}
} else {
	echo "lotnumbers was empty! :";
}

and you just cannot process that part of the form the way you are thinking about.
picture this:
lot 3, 12, and 16 selected so array is lot[0]=>3, lot[1]=>12, lot[2]=>16
lath is selected for 3 and 16, not 12, lath[0]=>1,182.75, lath[1]=>1,268.25 (there is no lath[2] you are only passing in 2 values)
now trying to process at this point your 'lath' code will match lot[0] 'correctly' as that happens to be correct, but it will try to put the value for lath16 into lath12 placeholder because it is the second value in the array
another scenario similar to above is if you did not select a 'lath' value for lot 3 and you did for lot 12 and lot 16... your array looks like lath[0]=>1,239.75 and lath[1]=>1,268.25 (the values of lot 12 and 16 are contained in the 0 and 1 places in your array throwing any parsing of this off because you would try to set the field next to lot3 with lath0

I'm not saying you can't do what it is that you are trying to accomplish, but this is not the way.

0

Yea I was trying to not have a bunch of different forms (there will be one for each jobsite). I'm going to have to give each one it's own variable from the looks of it :/

Thanks for your help though, I really appreciate it.

0

yes, you need to clearly identify each array a little better with some sort of id coming across, you might have to write more code to actually process the form because of this... I would suggest adding an 'id' or some such thing and then when you are processing call a function that will try to pull all of the values from the set of expected values coming in here I whipped up a little code, it works (only processing half the form though...

<table class="pstable">
    <tr>
    <td>Lot #</td>
    <td>Plan</td>
    <td>Yds</td>
    <td>Scaffold U</td>
    <td>Lath</td>
    <td>Stone</td>
    <td>Scratch</td>
    <td>Brown</td>
    <td>Foam</td>
    <td>Finish</td>
    <td>Prep</td>
    <td>Scaffold D</td>
    <td>Cleanup</td>
    </tr>
    <tr>
    <td><label><input type="checkbox" name="lot_3" value="3" />3</label></td>
    <td><label><input type="checkbox" name="plan_3" value="3B" />3B</label></td>
    <td><label><input type="checkbox" name="yards_3" value="415" />415</label></td>
    <td><label><input type="checkbox" name="scaffoldU_3" value="456" />456</label></td>
    <td><label><input type="checkbox" name="lath_3" value="1,182.75" />$1,182.75</label></td>
    <td><label><input type="checkbox" name="stone_3" value="560.00" />$560.00</label></td>
    <td><label><input type="checkbox" name="scratch_3" value="498.00" />$498.00</label></td>
    <td><label><input type="checkbox" name="brown_3" value="747.00" />$747.00</label></td>
    <td><label><input type="checkbox" name="foam_3" value="633.00" />$633.00</label></td>
    <td><label><input type="checkbox" name="finish_3" value="622.50" />$622.50</label></td>
    <td><label><input type="checkbox" name="prep_3" value="150.00" />$150.00</label></td>
    <td><label><input type="checkbox" name="scaffoldD_3" value="250.00" />$250.00</label></td>
    <td><label><input type="checkbox" name="cleanup_3" value="150.00" />$150.00</label></td>
    </tr>
    <tr>
    <td><label><input type="checkbox" name="lot_12" value="12" />12</label></td>
    <td><label><input type="checkbox" name="plan_12" value="1A" />1A</label></td>
    <td><label><input type="checkbox" name="yards_12" value="435" />435</label></td>
    <td><label><input type="checkbox" name="scaffoldU_12" value="479" />479</label></td>
    <td><label><input type="checkbox" name="lath_12" value="1,239.75" />$1,239.75</label></td>
    <td><label><input type="checkbox" name="stone_12" value="" /> - </label></td>
    <td><label><input type="checkbox" name="scratch_12" value="522.00" />$522.00</label></td>
    <td><label><input type="checkbox" name="brown_12" value="783.00" />$783.00</label></td>
    <td><label><input type="checkbox" name="foam_12" value="543.00" />$543.00</label></td>
    <td><label><input type="checkbox" name="finish_12" value="652.50" />$652.50</label></td>
    <td><label><input type="checkbox" name="prep_12" value="150.00" />$150.00</label></td>
    <td><label><input type="checkbox" name="scaffoldD_12" value="261.00" />$261.00</label></td>
    <td><label><input type="checkbox" name="cleanup_12" value="150.00" />$150.00</label></td>
    </tr>

I named the 'name' values according to the lot# that was associated with those particular values so lot[] became lot_3, and lot_12 for the example code:
and here is how I processed it on the other side:

<?php
// I've created a function to format the inner part of your td row follow below.
function format_values($lot,$plan,$yards,$scaffoldU,$lath,$stone,$scratch,$brown,$foam,$finish,$prep,$scaffoldD,$cleanup) {
	$td = '';
	if ($lot != '') {
		$td .= "<td>" . $lot . "</td>";
	} else {
		$td .= "<td></td>";
	}
	if ($lath != '') {
		$td .= "<td>" . $lath . "</td>";
	} else {
		$td .= "<td></td>";
	}		
	/*
	if ($plan != '') {
		echo "<td>" . $plan . "</td>";
	} else {
		echo "<td></td>";
	} 
	*/
	if ($stone != '') {
		$td .= "<td>" . $stone . "</td>";
	} else {
		$td .= "<td></td>";
	}			
	if ($scratch != '') {
		$td .= "<td>" . $scratch . "</td>";
	} else {
		$td .= "<td></td>";
	}
	if ($brown != '') {
		$td .= "<td>" . $brown . "</td>";
	} else {
		$td .= "<td></td>";
	}	
	if ($foam != '') {
		$td .= "<td>" . $foam . "</td>";
	} else {
		$td .= "<td></td>";
	}		
	if ($finish != '') {
		$td .= "<td>" . $finish . "</td>";
	} else {
		$td .= "<td></td>";
	}		
	if ($prep != '') {
		$td .= "<td>" . $prep . "</td>";
	} else {
		$td .= "<td></td>";
	}	
	if ($scaffoldD != '') {
		$td .= "<td>" . $scaffoldD . "</td>";
	} else {
		$td .= "<td></td>";
	}	
	if ($cleanup != '') {
		$td .= "<td>" . $cleanup . "</td>";
	} else {
		$td .= "<td></td>";
	}		
	return $td;
}


	$html = '';
	$lot = '';
	if (!empty($_REQUEST['lot_3'])) {
		$lot = $_REQUEST['lot_3'];
	}
	$plan = '';
	if (!empty($_REQUEST['plan_3'])) {
		$plan = $_REQUEST['plan_3'];
	}
	$lath = '';
	if (!empty($_REQUEST['lath_3'])) {
		$lath = $_REQUEST['lath_3'];
	}
	if (!empty($_REQUEST['yards_3'])) {	
		$yards = $_REQUEST['yards_3'];
	}
	$scaffoldU = '';
	if (!empty($_REQUEST['scaffoldU_3'])) {	
		$scaffoldU = $_REQUEST['scaffoldU_3'];
	}
	$stone = '';
	if (!empty($_REQUEST['stone_3'])) {	
		$stone = $_REQUEST['stone_3'];
	}
	$scratch = '';
	if (!empty($_REQUEST['scratch_3'])) {	
		$scratch = $_REQUEST['scratch_3'];
	}
	$brown = '';
	if (!empty($_REQUEST['brown_3'])) {	
		$brown = $_REQUEST['brown_3'];
	}
	$foam = '';
	if (!empty($_REQUEST['foam_3'])) {	
		$foam = $_REQUEST['foam_3']; 
	}
	$finish = '';
	if (!empty($_REQUEST['finish_3'])) {	
		$finish = $_REQUEST['finish_3'];
	}
	$prep = '';
	if (!empty($_REQUEST['prep_3'])) {	
		$prep = $_REQUEST['prep_3'];
	}
	$scaffoldD = '';
	if (!empty($_REQUEST['scaffoldD_3'])) {	
		$scaffoldD = $_REQUEST['scaffoldD_3'];
	}
	$cleanup = '';
	if (!empty($_REQUEST['cleanup_3'])) {			
		$cleanup = $_REQUEST['cleanup_3'];
	}
	$html .= "<table>
    <tr>
    <td>Lot # -</td>
    <td> Lath -</td>
    <td> Stone -</td>
    <td> Scratch -</td>
    <td> Brown -</td>
    <td> Foam -</td>
    <td> Finish -</td>
    <td> Prep -</td>
    <td> Scaffold D -</td>
    <td> Cleanup</td>
    </tr>
    <tr>";
	$html .= format_values($lot,$plan,$yards,$scaffoldU,$lath,$stone,$scratch,$brown,$foam,$finish,$prep,$scaffoldD,$cleanup);
	$html .="</tr><tr>";
	// rinse and repeat
	$lot = '';
	if (!empty($_REQUEST['lot_12'])) {
		$lot = $_REQUEST['lot_12'];
	}
	$plan = '';
	if (!empty($_REQUEST['plan_12'])) {
		$plan = $_REQUEST['plan_12'];
	}
	$lath = '';
	if (!empty($_REQUEST['lath_12'])) {
		$lath = $_REQUEST['lath_12'];
	}
	if (!empty($_REQUEST['yards_12'])) {	
		$yards = $_REQUEST['yards_12'];
	}
	$scaffoldU = '';
	if (!empty($_REQUEST['scaffoldU_12'])) {	
		$scaffoldU = $_REQUEST['scaffoldU_12'];
	}
	$stone = '';
	if (!empty($_REQUEST['stone_12'])) {	
		$stone = $_REQUEST['stone_12'];
	}
	$scratch = '';
	if (!empty($_REQUEST['scratch_12'])) {	
		$scratch = $_REQUEST['scratch_12'];
	}
	$brown = '';
	if (!empty($_REQUEST['brown_12'])) {	
		$brown = $_REQUEST['brown_12'];
	}
	$foam = '';
	if (!empty($_REQUEST['foam_12'])) {	
		$foam = $_REQUEST['foam_12']; 
	}
	$finish = '';
	if (!empty($_REQUEST['finish_12'])) {	
		$finish = $_REQUEST['finish_12'];
	}
	$prep = '';
	if (!empty($_REQUEST['prep_12'])) {	
		$prep = $_REQUEST['prep_12'];
	}
	$scaffoldD = '';
	if (!empty($_REQUEST['scaffoldD_12'])) {	
		$scaffoldD = $_REQUEST['scaffoldD_12'];
	}
	$cleanup = '';
	if (!empty($_REQUEST['cleanup_12'])) {			
		$cleanup = $_REQUEST['cleanup_12'];
	}	
	$html .= format_values($lot,$plan,$yards,$scaffoldU,$lath,$stone,$scratch,$brown,$foam,$finish,$prep,$scaffoldD,$cleanup);
	$html .="</tr>";  // if you need to continue, which you do, add another <tr> in here and continue like before:
	$html .="</table>";
	echo "html = <hr>" . $html;

This could get the job done for you, you would have to alter your existing code.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.