A JM Posted August 17, 2009 Share Posted August 17, 2009 If I have a listbox like so and I have a variable with the value of the item that I want to have selected how can this be done? $selectedItem = $_GET['querytype']; $selectBox = "<select id='querytype' name='querytype' onchange='loadWheres();'><option>Select Query Type</option>"; $selectBox = $selectBox.'<option value="number">Number</option>'; $selectBox = $selectBox.'<option value="name">Name</option>'; $selectBox = $selectBox.'<option value="address">Address</option>'; $selectBox = $selectBox.'<option value="state">State</option>'; $selectBox = $selectBox."</select>"; <script type="text/javascript"> function loadWheres() { var d = document.getElementById('querytype'); var value = d.value; document.location = 'filtered_qrys.php?querytype=' + escape(value); } </script> Quote Link to comment https://forums.phpfreaks.com/topic/170577-solved-how-to-select-item-in-lstbox/ Share on other sites More sharing options...
joel24 Posted August 17, 2009 Share Posted August 17, 2009 use if ($selectedItem == 'value') { echo ' selected="selected" '; } put that line of code in each <option> tag and change the value for each option Quote Link to comment https://forums.phpfreaks.com/topic/170577-solved-how-to-select-item-in-lstbox/#findComment-899711 Share on other sites More sharing options...
A JM Posted August 17, 2009 Author Share Posted August 17, 2009 Like? $selectBox = $selectBox.'<option value='number' <?php if ($selectedItem == 'number') { echo ' selected="selected" '; }?>>Number</option>'; Quote Link to comment https://forums.phpfreaks.com/topic/170577-solved-how-to-select-item-in-lstbox/#findComment-899719 Share on other sites More sharing options...
A JM Posted August 17, 2009 Author Share Posted August 17, 2009 I believe this is correct but I can't get the syntax correct, any help? $selectBox = $selectBox.'<option value=number. <?php if($querytype == number) {echo "selected=selected"; }?>>Number</option>'; Quote Link to comment https://forums.phpfreaks.com/topic/170577-solved-how-to-select-item-in-lstbox/#findComment-899761 Share on other sites More sharing options...
A JM Posted August 17, 2009 Author Share Posted August 17, 2009 I'm still trying to get this to work and having problems. $selectBox = "<select id='querytype' name = 'querytype' onchange='loadWheres();'><option>Select Query Type</option>"; $selectBox = $selectBox."<option value=number<?php if(". $querytype ." == number) {echo selected=selected; }?>Number</option>"; $selectBox = $selectBox."</select>"; From the source of the page. <select id='querytype' name = 'querytype' onchange='loadWheres();'><option>Select Query Type</option><option value=number <?php if(number == number) {echo selected=selected; }?>Number</option></select> Since the php is showing on the page I know I have the syntax wrong... Any help would be appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/170577-solved-how-to-select-item-in-lstbox/#findComment-899955 Share on other sites More sharing options...
DarkendSoul Posted August 17, 2009 Share Posted August 17, 2009 $selectBox = "<select id='querytype' name = 'querytype' onchange='loadWheres();'><option>Select Query Type</option>"; $selectBox .= "<option value=number"; if ($querytype == "number") { $selectBox .= " selected=\"selected\""; } $selectBox .= "?>Number</option>"; $selectBox .= "</select>"; Quote Link to comment https://forums.phpfreaks.com/topic/170577-solved-how-to-select-item-in-lstbox/#findComment-899962 Share on other sites More sharing options...
A JM Posted August 17, 2009 Author Share Posted August 17, 2009 Is there a reason this can't be done on a single line? Thanks, Quote Link to comment https://forums.phpfreaks.com/topic/170577-solved-how-to-select-item-in-lstbox/#findComment-899986 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.