In this article, we are going to look at how to implement an Ajax solution that uses a PHP page to pull data from a back-end database. In this tutorial, we have a table stored in a MySQL database.

You can replace the PHP code on this page with any other server-side scripting languages that you are familiar with such as ASP or ASP.NET. In addition, the back-end data source does not have to be MySQL.

You can modify the database connection in the example for accessing other database platforms. The concept remains the same.

HTML Example

Select an Employee for more details!

PHP Example

Ajax and PHP$conn = mysql_connect($hostname,$username, $password); if (!$conn) { die('Could not connect: ' . mysql_error()); } mysql_select_db($dbname); $query = "SELECT * FROM $usertable WHERE empID = '" .$_GET['q']."'"; $result = mysql_query($query); if($result){ while($row = mysql_fetch_array($result)){ echo "<tr><td style='width:100px;'>Name:</td><td>".$row["$field1"]."</td></tr>"; echo "<tr><td style='width:100px;'>Title:</td><td>".$row["$field2"]."</td></tr>"; echo "<tr><td style='width:100px;'>Office:</td><td>".$row["$field3"]."</td></tr>"; } } mysql_close($conn); ?> </table>

We used a front-end HTML web page that loads data from a PHP (.php) page via Ajax. When a user selects an item in the dropdown list, the selection is sent to the PHP page using Ajax and the receiving page uses the information passed in the query string.

The value of the query string parameter is sent to the database and the results are sent back to the HTML page on the XmlHttpResponse.responseText property.