Created
March 30, 2017 12:20
-
-
Save Rameshwar-ghodke/4d3aaae4bd3d0a71efcbcb63b7f63e68 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<script> | |
function showUser(str) { | |
if (str == "") { | |
document.getElementById("txtHint").innerHTML = ""; | |
return; | |
} else { | |
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 (this.readyState == 4 && this.status == 200) { | |
document.getElementById("txtHint").innerHTML = this.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> | |
//************************* PHP File getuser.php ********************************************** | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<style> | |
table { | |
width: 100%; | |
border-collapse: collapse; | |
} | |
table, td, th { | |
border: 1px solid black; | |
padding: 5px; | |
} | |
th {text-align: left;} | |
</style> | |
</head> | |
<body> | |
<?php | |
$q = intval($_GET['q']); | |
$con = mysqli_connect('localhost','peter','abc123','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> | |
<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); | |
?> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment