0

suppose i have a select HTML element on the page. how can i select the option after passing a query string. such as computer.php?type=super2 it should select the super2 option in the select

 <select>
 <option value="Super">Super</option>
 <option value="Super2">Super2</option>
 <option value="Super3">Super3</option>    
 <option value="Super4">Super4</option>
 </select>

can any one help. note: i need a static solution. i am not using any database. althou i am using PHP. any solution based on jquery or PHP ?

1
  • Getting query string param clue from here.. stackoverflow.com/questions/901115/…. and setting value for select clue .val("somevaluestring") Commented Sep 29, 2013 at 4:43

1 Answer 1

3

Here's how you can do it in PHP:

$type = $_GET['type'];
$options = array('Super', 'Super2', 'Super3', 'Super4');
echo '<select>';
foreach ($options as $option) {
    echo "<option value='$option'" . ($option == $type ? ' selected' : '') . ">$option</option>";
}
echo '</select>';
Sign up to request clarification or add additional context in comments.

1 Comment

thats awesom :) thank you very much.. it works like a charm. :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.