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? Link to comment https://forums.phpfreaks.com/topic/161421-programatically-selecting-an-item-in-a-list/ 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>'; ?> Link to comment https://forums.phpfreaks.com/topic/161421-programatically-selecting-an-item-in-a-list/#findComment-851859 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> Link to comment https://forums.phpfreaks.com/topic/161421-programatically-selecting-an-item-in-a-list/#findComment-851860 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; Link to comment https://forums.phpfreaks.com/topic/161421-programatically-selecting-an-item-in-a-list/#findComment-851865 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? Link to comment https://forums.phpfreaks.com/topic/161421-programatically-selecting-an-item-in-a-list/#findComment-851894 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; ?> Link to comment https://forums.phpfreaks.com/topic/161421-programatically-selecting-an-item-in-a-list/#findComment-852069 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.