Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Variable From Form Value

Status
Not open for further replies.

PCHomepage

Programmer
Feb 24, 2009
609
US
On a form using simple checkboxes that submit as an array SubData[], I am trying to convert the receiving end back into variables that can be used but can't quite get the syntax. The values are those in the globals but without the "Sub" and I am trying to prepend "Sub" to the results. What I have looks like it should work but it doesn't so clearly I've done somethign wrong. How can this be done?

Code:
global $SubGreenR;
global $SubRed;
global $SubBlue;
global $SubGreenB;
global $SubRGr;
global $SubBGb;
global $SubTotal;

if (isset($_POST['SubData'])):
	$SubData = $_POST['SubData'];
	foreach ($SubData as $key => $val):
		$SybData[$key][0] = ${"Sub".$val};
	endforeach;
endif;

In case the question is more easily answered with the form, here it is:

Code:
<form class="SubForm" name="SubForm" method="POST" action="testcheckbox.php">
GreenR:<input type="checkbox" name="SubData[]" id="SubData[]" value="GreenR">
 Red:<input type="checkbox" name="SubData[]" id="SubData[]" value="Red">
 Blue:<input type="checkbox" name="SubData[]" id="SubData[]" value="Blue">
 GreenB:<input type="checkbox" name="SubData[]" id="SubData[]" value="GreenB">
 RGr:<input type="checkbox" name="SubData[]" id="SubData[]" value="RGr">
 BGb:<input type="checkbox" name="SubData[]" id="SubData[]" value="BGb">
 Total:<input type="checkbox" name="SubData[]" id="SubData[]" value="Total">
 <input type="submit" value="FormTest" name="SubmitNew">
</form>

. . . and I am testing it by echoing the variables to the screen:

Code:
echo "<p><strong>Variables assigned:</strong>\n<p>";
echo "<p>GreenR: ".$SubGreenR."\n<br>";
echo "Red: ".$SubRed."\n<br>";
echo "Blue: ".$SubBlue."\n<br>";
echo "GreenB: ".$SubGreenB."\n<br>";
echo "RGr: ".$SubRGr."\n<br>";
echo "BGb: ".$SubBGb."\n<br>";
echo "Total: ".$SubTotal."\n<br>";
 
Other than an obvious typo, I just realized that this IS working after all! I simply wasn't assigning the variables a value and there is no need for the "Sub" as I was originally appending in onto the value, not the variable.

Code:
$SubData[$key][0] = ${$val};
${$val} = $val;

//OR
// ${$val} = "Sub".$val;

It works like a charm!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top