Jump to content

[SOLVED] How to SELECT item in lstbox?


A JM

Recommended Posts

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>

Link to comment
https://forums.phpfreaks.com/topic/170577-solved-how-to-select-item-in-lstbox/
Share on other sites

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.

$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>";

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.