I am trying to retrive value from database in dropdown and to display the datas related to the selected option.

I am new to php, Please Help me to write php code for the above .

Member Avatar

Ok, show us what you have and we'll guide you through it.


function showUser(str) {
  if (str=="") {
  if (window.XMLHttpRequest) {
    // code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
  } else { // code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  xmlhttp.onreadystatechange=function() {
    if (xmlhttp.readyState==4 && xmlhttp.status==200) {

<select name="users" onchange="showUser(this.value)">
<option value="">Select a person:</option>
<option value="1">Peter Griffin</option>
<option value="2">Lois Griffin</option>
<option value="3">Joseph Swanson</option>
<option value="4">Glenn Quagmire</option>
<div id="txtHint"><b>Person info will be listed here.</b></div>



$q = intval($_GET['q']);

$con = mysqli_connect('localhost','','','my_db');
if (!$con) {
  die('Could not connect: ' . mysqli_error($con));

$sql="SELECT * FROM user WHERE id = '".$q."'";
$result = mysqli_query($con,$sql);

echo "<table border='1'>

while($row = mysqli_fetch_array($result)) {
  echo "<tr>";
  echo "<td>" . $row['FirstName'] . "</td>";
  echo "<td>" . $row['LastName'] . "</td>";
  echo "<td>" . $row['Age'] . "</td>";
  echo "<td>" . $row['Hometown'] . "</td>";
  echo "<td>" . $row['Job'] . "</td>";
  echo "</tr>";
echo "</table>";


The above code retrives the value from database properly, but i am trying to change the option value in index.php as

while($row = mysql_fetch_assoc($q))
  $select .= "<option value='".$row['FirstName']."'>".$row['FirstName'].">    </option>";

this is not working. please help me. thanks in advance

Member Avatar

Am i right in thinking that this is nothing to do with ajax, you just need help creating the select field on page load filling it with data from your DB?

BTW - will FirstName be appropriate as a filter? Isn't is possible that many users will have the same first name?

since i'm not getting it, can u plz guide me

Member Avatar

I have no idea what you're trying to do.

I'm trying to fetch value from database into dropdown, and if a option is selected then it displays the related record in table format for selected option in dropdown

Plz guide me to do this

Thanks in advance

Member Avatar

This is an example - you need to change fieldnames, tablenames to suit yourself. In addition, I use PDO instead of mysqli (I don't like mysqli) - so you may need to modify that aspect slightly too.

$options = "<option value=''>Select a person:</option>\n";
$sql = "SELECT id, firstname, lastname FROM users";
foreach ($db->query($sql) as $row) {
    $options .= "<option value='{$row['id']}'>{$row['firstname']} {$row['lastname']}</option>\n";


<select id="users" name="users">
    <?php echo $options;?>

<div id="results"></div>

    var sel = document.getElementById('users');
        //run your ajax code and update the html table in #results
        //with data from the DB

Thank you very much.Fianlly i got it