I might be able to help here...
I have something similar in a page. A list populated from the database and then another info requested from the db depending on the selection.
I`m not really a programmer so the code is probably messy, but it works.
The javascript in the head part of the html:
Code: Select all
<script type="text/javascript">
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","getemail.php?q="+str,true);
xmlhttp.send();
}
</script>
The php code for the selection list:
Code: Select all
<?php
DEFINE ('DB_USER', 'user');
DEFINE ('DB_PASSWORD', 'password');
DEFINE ('DB_HOST', 'localhost');
DEFINE ('DB_NAME', 'users');
$con = @mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
if (!$con) {
echo 'Could not connect to MySQL: ' . mysqli_connect_error();
}
$sql="SELECT name, id FROM users ORDER BY name";
$result=mysqli_query($con, $sql);
?>
<select name="users" onchange="showUser(this.value)">
<option>Select your name</option>
<?php
while($row_list=mysqli_fetch_assoc($result)){
?>
<option value="<?php echo $row_list['id']; ?>"><?php echo $row_list['name']; ?></option>
<?php
}
?>
</select>
As you can see in the javascript I used another page (getemail.php) to inquire the db. Here the code for that:
Code: Select all
<?php
$a=$_GET["q"];
function email($a){$dbc = @mysqli_connect('localhost', 'user', 'password', 'database');
$sql2="SELECT email FROM users WHERE id=$a";
$list2=mysqli_query($dbc,$sql2);
$row_list2=mysqli_fetch_assoc($list2);
$nume=$row_list2['nume_fil'];
return $last;}
echo email($a);
?>
Put this where you want the email to appear:
As I said I compiled the page a long time ago and the code probably could be really improved, but you could start from here.