Load Records From Database With Dropdown Selection Without Submit Button
I've got two submit buttons in my form: Button to submit/save the form and insert the input fields to the database. Button to load the records into the form that are already in t
Solution 1:
If you are using jquery you can load the data on a drop-down change like so:
$('form select').change(function() {
alert('You should do your data loading now');
});
Please see the documentation
Solution 2:
<html><head><scripttype="text/javascript">functionshowUser(str)
{
if (str=="")
{
document.getElementById("txtHint").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=newXMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=newActiveXObject("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><selectname="users"onchange="showUser(this.value)"><optionvalue="">Select a person:</option><optionvalue="1">Peter Griffin</option><optionvalue="2">Lois Griffin</option><optionvalue="3">Glenn Quagmire</option><optionvalue="4">Joseph Swanson</option></select></form><br /><divid="txtHint"><b>Person info will be listed here.</b></div></body></html>
And the getuser.php
<?php$q=$_GET["q"];
$con = mysql_connect('localhost', 'peter', 'abc123');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("ajax_demo", $con);
$sql="SELECT * FROM user WHERE id = '".$q."'";
$result = mysql_query($sql);
echo"<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Hometown</th>
<th>Job</th>
</tr>";
while($row = mysql_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>";
mysql_close($con);
?>
Example of how it works.
Post a Comment for "Load Records From Database With Dropdown Selection Without Submit Button"