0

I have a form, with a simple select box, and I also have a text field that pulls a value. I want to use the value in $Defaultselection as the default value for the select options. So that if $Defaultselection = 4 then the default selected option would be D.

Using PHP/HTML

`$Defaultselection` 

contains an integer between 1-4.

<select >
    <option value="1">A</option>
    <option value="2">B</option>
    <option value="3">C</option>
    <option value="4">D</option>
</select> 
5
  • 2
    Please put your question with clearance Commented Aug 8, 2013 at 11:33
  • In which language do you want? Commented Aug 8, 2013 at 11:35
  • The page is a mix of html/php @PrashantParekh Commented Aug 8, 2013 at 11:40
  • yes that I understand, But I am not getting what you want to do with this? Commented Aug 8, 2013 at 11:43
  • I edited the question. Hopefully better Commented Aug 8, 2013 at 11:48

1 Answer 1

4

That should do it:

<select >
    <option value="1" <?php echo ($Defaultselection == 1)?"selected":""; ?>>A</option>
    <option value="2" <?php echo ($Defaultselection == 2)?"selected":""; ?>>B</option>
    <option value="3" <?php echo ($Defaultselection == 3)?"selected":""; ?>>C</option>
    <option value="4" <?php echo ($Defaultselection == 4)?"selected":""; ?>>D</option>
</select> 

or this other one:

<select >
    <option value="1" <?php if ($Defaultselection == 1) echo "selected"; ?>>A</option>
    <option value="2" <?php if ($Defaultselection == 2) echo "selected"; ?>>B</option>
    <option value="3" <?php if ($Defaultselection == 3) echo "selected"; ?>>C</option>
    <option value="4" <?php if ($Defaultselection == 4) echo "selected"; ?>>D</option>
</select> 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.