0

我有一个使用 html PHP 和 Mysql 动态创建的表。我想使用 Ajax 从数据库中检索信息。

我找到的最接近我想做的例子是 http://www.w3schools.com/PHP/php_ajax_database.asp但这使用了一个下拉列表。

任何人都可以提供有关如何执行上述操作但使用表格的一些信息,因此您可以单击一个单元格,包含名称的单元格将被发送到数据库以检索特定数据。

   <?php
$default = "<img src='http://localhost/on.png' width='24' height='24'/>";
$default1 = "<img src='http://localhost/of.png' width='24' height='24'/>";
$con = mysql_connect("*****","*****","****");
if (!$con){
die("Can not connect: " . mysql_error());
}
mysql_select_db ("****",$con);
$sql= "select act.*
from user_activity as act
  inner join (
  select user_id, max(timestamp) as max_ts
  from user_activity
  group by user_id) as a on act.user_id=a.user_id and act.timestamp=a.max_ts";
$mydata = mysql_query($sql,$con);

echo "<table id='tfhover',table border=0>
<tr>
<th>Users</th>
<th>Status<th>
</tr>";
while($record = mysql_fetch_array($mydata)){
echo "<tr>";
echo "<td>" . $record['user_id'] . "</td>";
if (strtolower(trim($record['activity']))!=strtolower('LOGIN')){ 
echo "<td>" . $default1  . "</td>";
}else{

echo "<td>" . $default  . "</td>";

}
4

2 回答 2

0

您可以将 ID 存储在数据属性中并根据请求使用它。

<table>
  <tbody>
    <tr>
      <td data-item-id="1">1</td>
      <td data-item-id="2">2</td>
    </tr>
  </tbody>
</table>

<script type="text/javascript">
  $('table').on('click', 'td', function() {
    var id = $(this).data('item-id');
    $.get('url', {id: id}, function() {
      //process
    });
  });
</script>
于 2012-10-28T16:39:44.763 回答
0

您也可以只onClick="showUser(str)"在表格的 tr 内使用。

这是一个经过编辑的示例:

<?php   
//Test Data
$result = array(
1=>array('FirstName'=>'Peter','Lastname'=>'Griffin','Age'=>41,'HomeTown'=>'Quahog','Job'=>'Brewery'),
2=>array('FirstName'=>'Lois','Lastname'=>'Griffin','Age'=>40,'HomeTown'=>'Newport','Job'=>'Piano Teacher'),
3=>array('FirstName'=>'Joseph','Lastname'=>'Swanson','Age'=>39,'HomeTown'=>'Quahog','Job'=>'Police Officer'),
4=>array('FirstName'=>'Glenn','Lastname'=>'Quagmire','Age'=>41,'HomeTown'=>'Quahog','Job'=>'Pilot'),
);

//Your AJAX Responce
if(isset($_GET["q"])){

    if(array_key_exists($_GET["q"],$result)){
        echo "<table border='1'><th>Firstname</th><th>Lastname</th><th>Age</th><th>Hometown</th><th>Job</th></tr>";
        echo "<tr>";
        echo "<td>" . $result[$_GET["q"]]['FirstName'] . "</td>";
        echo "<td>" . $result[$_GET["q"]]['Lastname'] . "</td>";
        echo "<td>" . $result[$_GET["q"]]['Age'] . "</td>";
        echo "<td>" . $result[$_GET["q"]]['HomeTown'] . "</td>";
        echo "<td>" . $result[$_GET["q"]]['Job'] . "</td>";
        echo "</tr>";
        echo "</table>";
    }else{
        echo "No Results";
    }
    die;
}
?> 

<html>
<head>
<script>
function showUser(str)
{
    if (str=="")
    {
        document.getElementById("txtHint").innerHTML="";
        return;
    }
    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 (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET","index.php?q="+str,true);
    xmlhttp.send();
}
</script>
</head>
<body>

<table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse" width="100%">
  <?php foreach($result as $key=>$value){?>
    <tr onClick="showUser(<?php echo $key;?>)">
        <td width="50%">Get <?php echo $value['FirstName'];?></td>
    </tr>
 <?php } ?>
</table>

<br />
<div id="txtHint"><b>Person info will be listed here.</b></div>

</body>
</html> 
于 2012-10-28T17:01:59.597 回答