2
Contributors
8
Replies
33
Views
3 Years
Discussion Span
Last Post by My question
0

index.php

<html>
<head>
<script>
function showUser(str) {
  if (str=="") {
    document.getElementById("txtHint").innerHTML="";
    return;
  }
  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) {
      document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
  }
  xmlhttp.open("GET","getuser.php?q="+str,true);
  xmlhttp.send();
}
</script>
</head>
<body>

<form>
<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>
</select>
</form>
<br>
<div id="txtHint"><b>Person info will be listed here.</b></div>

</body>
</html> 

getuser.php

<?php
$q = intval($_GET['q']);

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

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

echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Hometown</th>
<th>Job</th>
</tr>";

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

mysqli_close($con);
?> 

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

0

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?

0

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

0

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;?>
</select>

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

<script>
    var sel = document.getElementById('users');
    sel.onchange=function(){
        //run your ajax code and update the html table in #results
        //with data from the DB
    };
</script>
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.