1

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!

1
  • The foreach code you wrote should be fine. You might want to use echo ' selected="selected"'; instead of the non-XHTML-compliant echo ' selected'; though Commented Jun 2, 2010 at 22:11

3 Answers 3

2

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>";
?>
Sign up to request clarification or add additional context in comments.

7 Comments

I agree structured code is very important. Bad code is a lack of proffesionality in my opinion
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, yes if you want to distinguish what option was selected, the values need to be unique, like option1 ... option8
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, I´ve made two small modifications to your code, see above.
|
2

Try this:

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

Comments

-1

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

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.