Help with PHP and associative arrays
I have to do a simple calculator in php based on user's input and choice from select field, something like this:
<?php
$a = $_GET['a'];
$b = $_GET['b'];
$array = array( "option1" => 0.1,
"option2" => 0.15,
"option3" => 0.3,
"option4" => 3,
"option5" => 3,
"option6" => 16,
"option7" => 16,
"option8" => 16
);
echo "<form action='calc.php' 开发者_运维知识库method='get'>";
echo "<input type='text' name='a' value='".$a."'> of ";
echo "<select name='b'>";
foreach ($array as $k => $v) {
echo "<option value='".$v."'>".$k."</option>";
}
echo "</select> ";
echo "<input type='submit' value='='> ";
$total_volume = $a * $b;
echo $total_volume;
echo "</form>";
?>
Well, for now everything works fine, but the idea is that after user submits form, the page reloads with sent amount in input field and selected option which user actually selected...
First thing is easy: I just put value="a"
in my input field, but I'm not sure how to make a selected option in <select>
field???
I started with this:
foreach ($array as $k => $v) {
echo "<option value='".$v."'";
if ($b == $v) {
echo " selected ";
}
echo ">".$k."</option>";
}
...but this is obviously not working as expected... Please help me with this easy one :)
Thanks!
You need to echo something like 'selected="selected"'
. The rest of the code seems fine to me.
On second thought there is something structurally wrong as multiple options return the same value, making it impossible to select the right one after submitting the form.
You will need to send $k
as the value in the select in your loop and for your calculations you just use $array[$b]
instead of $b
.
<?php
$a = $_GET['a'];
$b = $_GET['b'];
$array = array( "option1" => 0.1,
"option2" => 0.15,
"option3" => 0.3,
"option4" => 3,
"option5" => 3,
"option6" => 16,
"option7" => 16,
"option8" => 16
);
echo "<form action='calc.php' method='get'>";
echo "<input type='text' name='a' value='".$a."'> of ";
echo "<select name='b'>";
foreach ($array as $k => $v) {
echo "<option value='".$k."'";
if ($b == $k) {
echo ' selected="selected"';
}
echo ">".$k."</option>"; // or $v if you want to show the number
}
echo "</select> ";
echo "<input type='submit' value='='> ";
$total_volume = $a * $array[$b];
echo $total_volume;
echo "</form>";
?>
Try this:
foreach ($array as $k => $v) {
$selected= ($b == $v) ? 'selected="selected"' : '';
echo "<option value='$v' $selected>$k</option>\n";
}
You have to catch the data in $_GET. Try
foreach ($_GET as $k => $v) {
echo "<option value='".$v."'";
if ($b == $v) {
echo " selected ";
}
echo ">".$k."</option>";
}
EDIT:
On my Computer it worked here is the dump of GET
<form action='' method='get'>
<input type='text' name='a' value='121'> of <select name='b'>
<option value='0.1'>option1</option>
<option value='0.15'>option2</option>
<option value='0.3'>option3</option>
<option value='3'>option4</option>
<option value='3'>option5</option><option value='16'>option6</option><option value='16'>option7</option><option value='16'>option8</option></select> <input type='submit' value='='> 363
</form>
The Url
http://localhost/test.php?a=121&b=3
And parameters
a 121 b 3
精彩评论