Loading Select List from DB using PHP

Discussion in 'PHP' started by Bostad, Aug 23, 2011.

  1. #1
    Hi;

    I'm having some problems with this, and can't find a web reference I understand. I'm new to web programming and PHP.

    I know this is quite simple, but what I want to do is populate a select pulldown menu from a database when the user selects what field they are looking for.

    Here's the form html code:

    <div id="HexCForm"><form name="HexCalc">
     Select: <select name="HCFS1" onchange="LoadHexForm(this.value)">
    <option>*</option>
    <option>SourceUID</option>
    <option>SourceName</option>
    <option>SourceFlag1</option>
    <option>DestUID</option>
    <option>DestName</option>
    <option>DestFlag1</option>
    </select><br>
    Search String: <div id = "HCFSearch"><select name="HCFS2">
    
    
    
    </select>
    
    
    Code (markup):
    and the client side AJAX script

    
    function LoadHexForm(str)  {
    
    xmlhttp=new XMLHttpRequest();
        
        xmlhttp.onreadystatechange=function()
      {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
        document.getElementById("HexCForm.HCFSearch").innerHTML=xmlhttp.responseText;
        }
      }
      xmlhttp.open("GET","loadlist.php?q="+str,true);
      xmlhttp.send();
      
      }
    
    Code (markup):
    and finally, the PHP script (with db connection stuff stripped out)

    
    $query = mssql_query("Select Distinct ".$q." from MasterCombatLog");
    
    // WHERE LEFT(SOURCEUID,11)='0x040000000'");
    
    if (!mssql_num_rows($query)) {
        echo 'No records found';
    } else {
    
    while ($row = mssql_fetch_assoc($query)) {
    
    $string = $row['.$q.'];
    
    echo "<option>-".$string."-</option>";
    }
    }
    mssql_free_result($query);
    
    Code (markup):
    Thanks for any assistance
     
    Bostad, Aug 23, 2011 IP
  2. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175