0

I am trying to have a drop down menu where you can choose from 3 options. I've got the following code, but I can't seem to get my third option to work. Any ideas?

        $_POST['faction'] = ($_POST['faction'] == "s")? "S" : "K";

I want to add a third option which is "R" and I can't get it to work.

Ideas please?

2
  • 1
    What do you mean dropdown? PHP is executed server side. Why not just repeatedly use if-else? Commented Dec 25, 2013 at 18:43
  • Are you trying to get drop down value in PHP ? Commented Dec 25, 2013 at 18:48

1 Answer 1

1

Although adding another ternary statement to your statement is possible I think it will affect the code readability.

Using array will make this simpler.

$option_array = array('r' => 'R', 's' => 'S', 'k' => 'K');

if (array_key_exists($_POST['faction'])) { 
    $_POST['faction'] = $option_array[$_POST['faction']]
}

You could choose to add the array_key_exists to make sure you are not updating $_POST['faction'] if $_POST['faction'] contains other values, but this is your choice.

Another approach similar to yours with multiple ternary operations:

$_POST['faction'] = ($_POST['faction'] == "s") ? ($_POST['faction'] == "r" ? "R" : "S") : "K";
Sign up to request clarification or add additional context in comments.

2 Comments

What it is that in my table, I've got the 3 options and from a drop down menu in a html code, it's selecting that option and then displaying it as information. I tried your code with the multiple ternary operations but that didn't work.
Please post output of var_dump($_POST)

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.