Page 1 of 1

Static listbox

Posted: Wed Jun 25, 2003 5:11 am
by JeffElkins

Code: Select all

echo "<select>";
echo " <option factor value=".28">Factor 1</option>";
echo " <option factor value="3.14">Factor 2</option>";
echo " <option factor value="2.41">Factor 3</option>";
echo "</select>";
echo "<br>"; var_dump($factor);
I need to do some math with one portion of the equation being based on a factor chosen via listbox.

In the snippet above, $factor always returns NULL. How can I put the listbox choice into the variable $factor?

Thanks!

Posted: Wed Jun 25, 2003 5:34 am
by volka
php is (almost always) server-side only.
If you want to send parameters to a script a new request client->server has to be performed.
To make this more obvious both parts have a file of their own in the example below
test.html:

Code: Select all

&lt;html&gt;
	&lt;head&gt;&lt;title&gt;document #1 containing the form/select&lt;/title&gt;&lt;/head&gt;
	&lt;body&gt;
		&lt;form method="GET" action="doc2.php"&gt;
			&lt;select name="factor"&gt;
				&lt;option value=".28"&gt;Factor 1&lt;/option&gt;
				&lt;option value="3.14"&gt;Factor 2&lt;/option&gt;
				&lt;option value="2.41"&gt;Factor 3&lt;/option&gt;
			&lt;/select&gt;
			&lt;input type="submit" /&gt;
		&lt;/form&gt;
	&lt;/body&gt;
&lt;/html&gt;
doc2.php:

Code: Select all

<html>
	<head><title>document #2 receiving the values of doc #1</title></head>
	<body>
		<?php echo isset($HTTP_GET_VARS['factor']) ? $HTTP_GET_VARS['factor'] : 'factor not set'; ?>
	</body>
</html>

Posted: Wed Jun 25, 2003 7:14 am
by JeffElkins

Code: Select all

<?php

////////////////////////
function doForm() {
////////////////////////

echo "<u><b>Tank Duration Calculation</b></u>";

echo "<form method="post" action="$PHP_SELF" METHOD=post">";

echo "<br> LPM : <input type="text" name="lpm" size="5" maxlength="5">";
echo "<br>";
echo "<br> PSI : <input type="text" name="psi" size="5" maxlength="5">";

echo "<br><br>";
echo "<select>";                                                                                   
echo " <option factor value=".28">E Cylinder</option>";                                                                                                
echo " <option factor value="3.14">H Cylinder</option>";                                                   
echo " <option factor value="2.41">G Cylinder</option>";                                                    
echo "</select>";

echo "<input type="hidden" name="op" value="calc">";
echo "<br><br><input type="submit" name="tst" value="calculate">";


}

/////////////////////////////////////
function doCalc($factor,$psi,$lpm) {
////////////////////////////////////

$result = $psi*$factor/$lpm;

echo "<br><br><br>";
echo "<u><b>PSI x Factor / LPM</b></u>"."<br>";
echo "PSI: ".$psi;
echo "<br>";
echo "LPM: ".$lpm;
echo "<br>";
echo "Factor: ".$factor;
echo "<br><br>";
echo " Minutes remaining: ".$result;

}


switch($op) {

case 'calc';
doCalc($factor,$psi,$lpm);
break;

default:
doForm();
break;

}

?>
Thanks for the reply.

I'm not using individual .html files, would it be possible for everything to be self-contained in one .php file? In the example above, I present the form in the default function, then process it in the doCalc() function. The $lpm and $psi varibles are passed to doCalc() but the variable $factor is not.

Sorry if I'm a bit confused. I don't quite understand why the user can input the first two variables successfully, but the third value (from the listbox) is ignored.

Posted: Wed Jun 25, 2003 7:57 am
by volka
you have to give the <select>-element a name poroperty, not the <option>s

Code: Select all

<select name="factor">
	<option factor value=".28">E Cylinder</option>
	<option factor value="3.14">H Cylinder</option>
	<option factor value="2.41">G Cylinder</option>
</select>

Posted: Wed Jun 25, 2003 8:07 am
by JeffElkins
Thanks, that did the trick.