tags:

views:

79

answers:

2

My associative array.

$money = array("Nickels" => $_POST["nickels"], "Dimes" => $_POST["dimes"], "Quarters" =>$_POST["quarters"]);

My html form set up to handle Quarters, Dimes and Nickels are not shown in this case for brevity.

<label><b>Quarters:</b></label>
<select name="quarters" >
<option value=".25">25c</option>
<option value=".50">50c</option>
<option value=".75">75c</option>
<option value="1">$1.00</option>
</select>

A user can only select either Quarters only, Dimes only, or Nickels only. If a user selects Quarters with the option value of .25, this value will be sent to the php script.

So I was just wondering for calculations based on the fact that the user can select Quarters only with one value, Dimes only with one value, and Nickels only with one value, and not a combination of denominations,

how would one go about setting up different test cases, for example if the user selects $money["Quarters"]; // With different values coming from the html form as .25, .50,.75, or 1, and only one of the selected values will make it through to the php script depending on what the user selected.

Can I do this:

switch($selection)
{
     case “Quarters”:
      echo “ You chose  $money[‘Quarters’];   .<br />”;
      break;
     case “Nickels”:
      echo “You chose $money[‘Nickels’]; .<br />”;
             break;
     case “Dimes”:
      echo “You chose  $money[‘Dimes’]; . <br />”;
                    break;
default: print  “Please select a Denomination”;
}

Thank you for not flaming the newb, I am still learning, and sorry for the mix and match in terms of " and .

+1  A: 

Selected values in a form are submitted as $_POST['quarters'].

I understand, that you want to check, if the user has selected more than one of your <select>s (correct?)

So, I'd create a check like this:

$selected = 0;

if ($_POST['quarters'] != "DEFAULT_VALUE_OF_YOU_SELECT_QUARTERS")
{
 $selected++;
}

if ($_POST['nickels'] != "DEFAULT_VALUE_OF_YOU_SELECT_NICKELS")
{
 $selected++;
}

if ($_POST['dimes'] != "DEFAULT_VALUE_OF_YOU_SELECT_DIMES")
{
 $selected++;
}

if ($selected > 1)
{
 // The user has selected more than one
}
ApoY2k
Thanks for the if structure, is there a way to just restrict the user from selecting multiple money denominations to just one, at a given time? For example, at any given moment the user should be only able to use just Quarters and not Quarters and some other combination.
Newb
Can I change if ($selected to <=1), so that the user only has one option? I don't want to have the user select different options, this might mess up my calculations for the different values that I have to start accounting for on the form?
Newb
Never-mind will figure out, thanks for the help.
Newb
Yes, that is possible, but only using JavaScript. Without, you always have to do the check first.
ApoY2k
Thanks you again for the info, I will put JS on my list of things to learn.
Newb
A: 

There's a few things to pay attention to here.

So, first of all, your $money array captures every value the user submits.

Next, the way you have your HTML <select> statement set up, there's no default value. The first option in $_POST["quarters"] is going to be .25 even if the user never touches that pulldown. To avoid this, you would want to add to the Quarters <select>:

<option value="0">-- Select Quarters --</option>

But still this doesn't allow you to use a switch/case statement. You're still going to be submitting a value for EVERY HTML <select> tag. ApoY2k's solution is better, since it checks every pulldown, making sure they only used one, and perhaps displays a message if they don't.

Finally... I don't know what $selection refers to in your example. You're populating the $money array, so the $selection variable doesn't exist.

I can further help if you clarify your question.

danieltalsky
I was initially going to use if, else, and else if's, but I was hoping I could use a switch, I guess not.What I wanted to know is which value the user had selected and do calculations on that value.
Newb
I will add the option for <option value ="0"> for each denomination, thank you.
Newb