views:

63

answers:

3

Hello.
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!

+2  A: 

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>";
?>
jeroen
I agree structured code is very important. Bad code is a lack of proffesionality in my opinion
streetparade
I think that your second thought is the closest to my solution :) I should definitely send string of selected option with my form, right?
errata
@errata, yes if you want to distinguish what option was selected, the values need to be unique, like option1 ... option8
jeroen
Yes, it occurred to me also :) But how could I send $k with form in my situation so I could fetch it with $_GET at the end?
errata
@errata, I´ve made two small modifications to your code, see above.
jeroen
@errata You don't. You keep track of that on the server and match the form data key (value="") with the list you store server-side ($array). Otherwise I could change the HTML on your page and submit whatever data I wanted.
banzaimonkey
brilliant! many many thanks!
errata
You're welcome!
jeroen
@banzaimonkey could you explain it little more maybe? do you have some link where i could read about my specific problem?
errata
+2  A: 

Try this:

foreach ($array as $k => $v) {
   $selected= ($b == $v) ? 'selected="selected"' : '';
   echo "<option value='$v' $selected>$k</option>\n";
}
dnagirl
A: 

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&amp;b=3

And parameters

a 121 b 3

streetparade
Sorry, but this is just completely wrong. Submitting 2 values will get you 2 options with your code with values **121** and **3** and text **a** and **b**.
jeroen