簡體   English   中英

使用Ajax通過錨點標記onclick事件從mysql獲取數據

[英]Fetching data from mysql through anchor tag onclick event using ajax

我希望能夠使用ajax在同一頁上的表中顯示來自mysql的數據。 當用戶單擊value = seatid的錨點鏈接時,該表將顯示一個使用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>&nbsp;</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);
?> 

您的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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM