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
JeffElkins
Forum Newbie
Posts: 7 Joined: Tue Jun 24, 2003 12:50 pm
Post
by JeffElkins » Wed Jun 25, 2003 5:11 am
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!
volka
DevNet Evangelist
Posts: 8391 Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger
Post
by volka » Wed Jun 25, 2003 5:34 am
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
<html>
<head><title>document #1 containing the form/select</title></head>
<body>
<form method="GET" action="doc2.php">
<select name="factor">
<option value=".28">Factor 1</option>
<option value="3.14">Factor 2</option>
<option value="2.41">Factor 3</option>
</select>
<input type="submit" />
</form>
</body>
</html>
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>
JeffElkins
Forum Newbie
Posts: 7 Joined: Tue Jun 24, 2003 12:50 pm
Post
by JeffElkins » Wed Jun 25, 2003 7:14 am
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.
volka
DevNet Evangelist
Posts: 8391 Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger
Post
by volka » Wed Jun 25, 2003 7:57 am
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>
JeffElkins
Forum Newbie
Posts: 7 Joined: Tue Jun 24, 2003 12:50 pm
Post
by JeffElkins » Wed Jun 25, 2003 8:07 am
Thanks, that did the trick.