Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用ajax通过锚定标记onclick事件从mysql获取数据_Javascript_Php_Mysql_Ajax - Fatal编程技术网

Javascript 使用ajax通过锚定标记onclick事件从mysql获取数据

Javascript 使用ajax通过锚定标记onclick事件从mysql获取数据,javascript,php,mysql,ajax,Javascript,Php,Mysql,Ajax,我希望能够使用ajax在同一页面的表中显示来自mysql的数据。当用户单击value=seatid的锚定链接时,表格将显示使用seatid显示名称的表格。但我无法显示姓名,当我点击链接时,它只显示标题为“姓名”的表格,而不是座位上的实际姓名。。我正在使用php、javascript、ajax和mysql。。。代码有问题吗,请帮我 <html> <head> <title>Seat Mapper System</title> <style>

我希望能够使用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>&nbsp;</td>";
    }
}
echo "</tr></table></td>";
echo "</tr>";
echo "</table>";

/* Close connection to database server. */
mysql_close();
?>
</body>
</html>

座椅映射系统
*{
字体大小:16px;
颜色:黑色;
字体系列:tahoma;}
运输署{
宽度:75px;
高度:35px;
}
运输署署长{
文字装饰:无;
显示:块;
宽度:100%;
身高:100%;
}
功能显示信息(str){
如果(str==“”){
document.getElementById(“txtInfo”).innerHTML=“”;
返回;
}
if(window.XMLHttpRequest)
{//IE7+、Firefox、Chrome、Opera、Safari的代码
xmlhttp=新的XMLHttpRequest();
}
其他的
{//IE6、IE5的代码
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
xmlhttp.onreadystatechange=函数(){
if(xmlhttp.readyState==4&&xmlhttp.status==200){
document.getElementById(“txtInfo”).innerHTML=xmlhttp.responseText;
}
}
open(“GET”,“getinfo.php?q=“+str,true”);
xmlhttp.send();
}
此处将列出人员信息。

您的AJAX代码工作正常,但getinfo.php页面有一些错误

您已经使用了mysql,但它已被弃用。所以你需要考虑使用MySQL或PDO。

mysql\u select\u db
只需要一个参数(数据库名称),您已经给出了2个

检查查询是否正确执行。使用mysql\u查询($con,$sql)或die(mysql\u 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
echo”“;
^^^^^^^将此更改为$seatid

您的大部分php代码似乎与HTML位于同一页面上。要单击按钮请求数据,通常单击会导致返回json数据的ajax调用,json数据将被解析并分发到各个元素。我想我们需要更多的信息。你是在尝试更新旧软件吗?这只是一个原型,我计划在我完成这项工作后分离php代码。我不知道如何使用json,我还是一个初学者,我在这里没有使用jquery,我在这里意外地标记了jqeury。当我单击锚标记链接时,onclick事件将从seatmapping数据库中查询名称和seatid,并将其显示在同一页面上,但我无法从数据库中显示名称和seatid,但它显示了标题为“name”和“seatnumber”的表。我尝试仅使用数据库名称作为参数,但仍然不起作用。。我检查查询以连接到数据库,它工作正常。虽然结果是名称和seatid,但它不会显示。请尝试从浏览器中运行getinfo.php?q=some\u correct\u值。这肯定不是Ajax错误,因为正在显示表。在mysql\u中,选择\u db和mysql\u query use or die(mysql\u error()),如答案中所示。另外,在php代码中,您还没有检查$_GET['q']是否为空。非常感谢您的帮助。我尝试使用or die(mysql_error()),但现在当我单击链接时,该表将不会显示。再次感谢您的帮助,该表现在显示查询结果,但“getinfo.php?q=”必须设置为值,例如1,才能显示结果。但是当我将它的值设置为一个变量时,在我的例子str中,它表示“没有要提取的行”。但是我希望能够根据我单击的链接显示名称和seatid。我开始认为标记超链接可能有问题,因为分配给变量的值来自于标记超链接。或者我可能错了…@rez Yep,你是对的。现在我注意到了。当我在本地尝试时,我硬编码了参数。您现在可以检查编辑。
echo "<a href=\"#\" onclick=\"showInformation($seatid)\" value=\"$seatid\"><b>$seatid</b></a>";
                                              ^^^^^^^ Change this to $seatid