我希望能够使用ajax在同一页面上的表中显示mysql中的数据。当用户单击带有value = seatid的锚链接时,该表将显示使用seatid显示名称的表。但我无法显示名称,当我点击链接时,它只显示带有标题“名称”的表,但不是那个海滩上的人的实际名称..我正在使用php,javascript,ajax和mysql ...代码有什么问题,请帮帮我......
<html>
<head>
<title>Seat Mapper System</title>
<style>
*{
font-size: 16px;
color: black;
font-family: tahoma;}
td td{
width: 75px;
height: 35px;
}
td td a{
text-decoration: none;
display: block;
width: 100%;
height: 100%;
}
</style>
<script>
function showInformation(str){
if (str==""){
document.getElementById("txtInfo").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("txtInfo").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","getinfo.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
<div id="txtInfo"><b>Person info will be listed here.</b></div>
<?php
$linkID = @ mysql_connect("localhost", "root", "*******") or die("Could
not connect to MySQL server");
@ mysql_select_db("seatmapping") or die("Could not select database");
/* Create and execute query. */
$query = "SELECT * from seats order by rowId, columnId desc";
$result = mysql_query($query);
$prevRowId = null;
$seatColor = null;
$tableRow = false;
//echo $result;
echo "<table width='100%' border='0' cellpadding='0' cellspacing='0'>";
while(list($rowId,$columnId,$status,$updatedby,$name,$seatid)=mysql_fetch_row($result))
{
if ($prevRowId != $rowId) {
if ($rowId != 'A') {
echo "</tr></table></td>";
echo "\n</tr>";
}
$prevRowId = $rowId;
echo "\n<tr><td align='center'>;
echo<table border='0' cellpadding='0' cellspacing='3'><tr>";
}
else {
$tableRow = false;
}
if ($status == 0) {
$seatColor = "99FF33";
} else if ($status == 1 && $updatedby == 'user1') {
$seatColor = "FFCC99";
} else if ($status == 2 && $updatedby == 'user1') {
$seatColor = "FF9999";
} else {
$seatColor = "red";
}
echo "\n<td bgcolor='$seatColor' align='center'>";
echo "<a href=\"#\" onclick=\"showInformation(this)\" value=\"$seatid\"><b>$seatid</b></a>";
echo "</td>";
if (($rowId == 'A' && $columnId == 7) || ($rowId == 'B' && $columnId == 7))
{
// This fragment is for adding a blank cell which represent the "center aisle"
echo "<td> </td>";
}
}
echo "</tr></table></td>";
echo "</tr>";
echo "</table>";
/* Close connection to database server. */
mysql_close();
?>
</body>
</html>
这是getinfo.php页面:
<?php
$q = intval($_GET['q']);
$con = mysql_connect('localhost','root','Newpass123#','seatmapping');
if (!$con)
{
die('Could not connect: ' . mysql_error($con));
}
mysql_select_db($con,'seatmapping');
$sql="SELECT name, seatid FROM seats WHERE seatid = '".$q."'";
$result = mysql_query($con,$sql);
echo "<table border='1'>
<tr>
<th>Name</th>
<th>Seat Number</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['seatid'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
答案 0 :(得分:1)
您的AJAX代码工作正常,但您的getinfo.php页面有一些错误。
您使用过mysql,但它已被弃用。所以你需要考虑使用mysqli或PDO。
mysql_select_db
只需要一个参数(数据库名称),您已经给出了2。
检查查询是否已正确执行。使用mysql_query($con,$sql) or die(mysql_error())
重写了getinfo.php,错误回应。
<?php
if(isset($_GET['q'])){
$q = intval($_GET['q']);
$con = mysql_connect('localhost','root','Newpass123#','seatmapping');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db('seatmapping');
$sql="SELECT name, seatid FROM seats WHERE seatid = ".$q;
$result = mysql_query($sql) or die("query err " . mysql_error());
if(mysql_num_rows($result) == 0){
echo "No rows to be fetched.";
}else{
echo "<table border='1'>
<tr>
<th>Name</th>
<th>Seat Number</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['seatid'] . "</td>";
echo "</tr>";
}
echo "</table>";
}
mysql_close($con);
}else{
echo "GET variable q is not set.";
}
?>
在您的HTML(第一个代码)中,您传递的参数是“this”,但在您的showInformation()中,您将其视为字符串。你可以直接传递$ seatid。
echo "<a href=\"#\" onclick=\"showInformation($seatid)\" value=\"$seatid\"><b>$seatid</b></a>";
^^^^^^^ Change this to $seatid