I would like to create a drop down list that will reflect values stored in a MySQL database. The table is a user table that has an id, firstname and last name fields. I would like the dropdown list to reflect all the id values from the table and a text box should output the respective first name and last name. I have created a drop down list which I am able to see all the values of the id field but the text box only reflects the name of the last value of the table. Could some one help me with this problem?
See snippet of code below:

//code starts

<?php
include("db.php");

echo "<select name=\"mid\">\n";
echo "<option value=\"\">Select Value</option>\n";

$strQuery=("SELECT * FROM member ORDER BY memberid ASC");
$rsrcResult = mysql_query($strQuery);

while($row = mysql_fetch_assoc($rsrcResult)) {
$strA = $row["id"];
$strB = $row["memberid"];
$fname=$row["firstname"];
$lname=$row["lastname"];
echo "<option value=\"$strA\">$strB</option>\n";
}

echo "</select>";
echo "<input type=\"text\" name=\"firstname\" value=\"".$fname." ".$lname."\" />";

?>
//code ends

I am able to see all the member ids but the textbox only reflects the first name and lastname of the last entry in the table. Could anyone shed some light on this please? Thanks

    write the connection for yours, and the SELECT * FROM users with the field names u use...:
    and put this codes into a .php file. and then you just replace the textfield into your dropdown list (at the very end of the code in the form ...)

    <?
    include("connect.php");
    if($_GET["hint"]==1)
    {
    //set IE read from page only not read from cache
    header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
    header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
    header ("Cache-Control: no-cache, must-revalidate");
    header ("Pragma: no-cache");
    
    header("content-type: application/x-javascript; charset=iso-8859-2");
    
    $q=(int)$_GET["q"];
    	$parancs="SELECT * FROM users WHERE `users_id`=$q LIMIT 1";
    	$eredmeny=mysql_query($parancs);
    	if(!$eredmeny)
    	die(mysql_error());
    
    if (mysql_num_rows($eredmeny)>0)   //if it has a value...
    {
    $row=mysql_fetch_assoc($eredmeny);
    print '<input type="text" id="lastname-firstname" value="'.$row["lastname"]." -  	".$row["firstname"].'" name="lastname_firstname">';
    }
    else
    print '<input type="text" id="lastname-firstname" name="lastname_firstname" value="">';
    die(0);
    }
    
    
    
    ?><script language="JavaScript" type="text/JavaScript">
    
    var xmlHttp1
    function showHint1(str)
    {
    if (str.length==0)
    {
    document.getElementById("txtHint1").innerHTML="";
    return;
    }
    xmlHttp1=GetXmlHttpObject1();
    if (xmlHttp1==null)
    {
    alert ("Your browser does not support AJAX!");
    return;
    }
    var url="<?=$_SERVER["PHP_SELF"]?>";
    url=url+"?q="+str;
    url=url+"&sid="+Math.random();
    url=url+"&hint=1";
    xmlHttp1.onreadystatechange=stateChanged1;
    xmlHttp1.open("GET",url,true);
    xmlHttp1.send(null);
    }
    
    function stateChanged1()
    {
    if (xmlHttp1.readyState==4)
    {
    document.getElementById("txtHint1").innerHTML=xmlHttp1.responseText;
    }
    }
    
    function GetXmlHttpObject1()
    {
    var xmlHttp1=null;
    try
    {
    // Firefox, Opera 8.0+, Safari
    xmlHttp1=new XMLHttpRequest();
    }
    catch (e)
    {
    // Internet Explorer
    try
    {
    xmlHttp1=new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e)
    {
    xmlHttp1=new ActiveXObject("Microsoft.XMLHTTP");
    }
    }
    return xmlHttp1;
    }
    
    </script>
    <form name="form2" method="post" action="?"><table border="1">
    <tr>
    <td>id</td><td>lastname-firstname</td></tr>
    
    <tr><td>
    put here a dropdown menu with an onkeyup, like in this textfield... and the name of the field will be id, and its value will be the table"s ID field value...
    <input type="text" id="id" name="id" onkeyup="showHint1(this.value)" value=""> 
    
     </td><td><p> <span id="txtHint1"></span></p></td></tr>
    </table> <input name="table2" type="hidden" id="table" value="users"> <br> <input type="submit" name="Submit2" value="Submit"> </form>

      Thanks for the information but its not working. I made two changes according to your instructions:

      1) $parancs="SELECT * FROM member WHERE memberid=$q LIMIT 1";

      2) <select id="id" name="id" onkeyup="showHint1(this.value)" value="<?php echo $row['memberid'];?>">
      <option value="">Please Select ID</option>
      </select>

      Is this correct? If not could you kindly give clearer instructions?
      Thanks!

        lets change this input field

        <select id="id" name="id" onkeyup="showHint1(this.value)" value="<?php echo $row['memberid'];?>">

        into a dropdown list...

        But in your table, what is the difference with the two field ?

        $strA = $row["id"];
        $strB = $row["memberid"];

        ?

        spoilchile;10891691 wrote:

        Thanks for the information but its not working. I made two changes according to your instructions:

        1) $parancs="SELECT * FROM member WHERE memberid=$q LIMIT 1";

        2) <select id="id" name="id" onkeyup="showHint1(this.value)" value="<?php echo $row['memberid'];?>">
        <option value="">Please Select ID</option>
        </select>

        Is this correct? If not could you kindly give clearer instructions?
        Thanks!

          $strA represents an auto increment id field...the other variable represents a 5-digit userid field

            Write a Reply...