The way AJAX works is that with the first request you write the basic HTML of your web page, including some javascript that calls back to the server and asks for more data. Depending on how you plan to send your data, it may make one or more requests after the page is rendered to get more data. Using AJAX will require that you rethink about how you're delivering your data. For example, you'll need one "script" to load the page, then another "script" to get the data -- of course, they could be the same, just with different parameters. I'll add a simple example to demonstrate since refactoring your example would require more understanding of your data and how it's delivered. This example is from w3schools.com.
HTML:
<script type="text/javascript">
$(function() {
$('#users').change(function() {
// here's the AJAX bit
$.get( '/users/load.php?q=' + $(this).val(),
function(html) {
$('#txtHint').html(html);
});
});
});
</script>
</head>
<body>
<form>
<select name="users">
<option value="">Select a person:</option>
<option value="1">Peter Griffin</option>
<option value="2">Lois Griffin</option>
<option value="3">Glenn Quagmire</option>
<option value="4">Joseph Swanson</option>
</select>
</form>
<br />
<div id="txtHint"><b>Person info will be listed here.</b></div>
</body>
</html>
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 = '" . mysql_real_escape_string( $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);
?>