<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8"/>
<script type="text/javascript" src="[URL unfurl="true"]http://code.jquery.com/jquery-1.10.1.min.js"></script>[/URL]
<script type="text/javascript">
$(document).ready(function(){
/* attach event listeners to the buttons */
$('.myButton').on('click', function(e){
e.preventDefault();
var id=$(this).attr('actionid');
/* make ajax call */
$.ajax(
{
async: false,
url: 'ajaxServer.php',
type: 'POST',
dataType: 'json',
data: {id:id},
success: function(data){
if(data.result == 'ok'){
alert(data.data);
}else{
alert(data.error);
}
}
});
});
});
</script>
</head>
<body>
<?php
doList();
?></body>
</html>
<?php
include_once 'dbconnect.php';
function doList(){
$query = "Select * from users where deleted != 1";
$result = mysql_query($query) or die(mysql_error());
$first = true;
$cellFormat = "<td>%s</td>";
$rowFormat = "<tr>%s</td>";
$headingCellFormat = "<th>%s</th>";
$headingFormat = "<thead>%</thead>";
$buttonFormat = '<button class="myButton" actionid="%s">Lookup</button>';
if(mysql_num_rows() > 0) echo '<table>';
while($row = mysql_fetch_assoc($result)):
if($first):
$count = count($row);
$headings = array_keys($row);
$output = '';
foreach($headings as $heading):
$output .= sprintf($headingCellFormat, htmlspecialchars($heading));
endforeach;
$output .= sprintf($headingCellFormat, 'Action');
$output = sprintf($headingFormat, sprintf($rowFormat, $output));
echo $output;
$first = false;
endif;
$output = '';
foreach($row as $item):
$output .= sprintf($cellFormat, htmlspecialchars($item));
endforeach;
$button = sprintf($buttonFormat, $row['id']);
$output .= sprintf($cellFormat, $button);
$output = sprintf($rowFormat, $output);
echo $output;
endwhile;
if(!$first) echo '</table>';
}
?>