grs5211 Posted June 8, 2009 Share Posted June 8, 2009 I have built a list with this code: <select id="moduleBox" name="selModule" style="align: center;" onChange="insertCode()"> <option value="A">Apple</option> <option value="L">Lemon</option> <option value="P">Peach</option> </select> I have brought over a query string variable from another page which has the value I want selected in ="selModule". Lets say the $_REQUEST("value") = "Lemon" How do I show this as the selected value in the listbox? Quote Link to comment Share on other sites More sharing options...
gijew Posted June 8, 2009 Share Posted June 8, 2009 Off the top of my head you should be using $_GET['value'] instead (security thing). Then all you have to do is write an if/else like... <?php if ($_GET['value'] == $option_value) { $selected = 'selected="selected"'; } else { $selected = ''; } echo '<option value="whatever" ' . $selected . '>Label</option>'; ?> Quote Link to comment Share on other sites More sharing options...
ldougherty Posted June 8, 2009 Share Posted June 8, 2009 does this work? <option value='$_REQUEST[value]' SELECTED><?php echo $_REQUEST[value]?></option> Quote Link to comment Share on other sites More sharing options...
Alex Posted June 8, 2009 Share Posted June 8, 2009 Off the top of my head you should be using $_GET['value'] instead (security thing). Then all you have to do is write an if/else like... <?php if ($_GET['value'] == $option_value) { $selected = 'selected="selected"'; } else { $selected = ''; } echo '<option value="whatever" ' . $selected . '>Label</option>'; ?> Just to shorten that, to make it look a little nicer: $selected = ($_GET['value'] == $option_value) ? 'selected="selected" : NULL; Quote Link to comment Share on other sites More sharing options...
grs5211 Posted June 8, 2009 Author Share Posted June 8, 2009 I get what you are saying here, but this assumes there is a processing loop to go through, which there is not. Lets say we are 'hard coding' every fruit known(thats a lot). We can't use the code at every <OPTION> line to decide if that is the one we want selected. Should we not do this after the <SELECT> is built? Quote Link to comment Share on other sites More sharing options...
sasa Posted June 9, 2009 Share Posted June 9, 2009 try <?php $options = '<select id="moduleBox" name="selModule" style="align: center;" onChange="insertCode()"> <option value="A">Apple</option> <option value="L">Lemon</option> <option value="P">Peach</option> </select>'; $options = str_replace('value="'.$_REQUEST['value'].'"', 'value="'.$_REQUEST['value'].'" selected="selected" ', $options); echo $options; ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.