简体   繁体   中英

Php ajax database info fetch returning nothing from batabase

I am using ajax to fetch information for my database and im running into a problem. nothing is being shown in return.

I have a select list that calls for a js script to run onchange, that calls for the php file to get the database information and return it in a table.

the problem is that upon the onchange event, I have a table show up with just the table heads but no information below. I checked console and the error Im getting is this:

GET http://lineofcode.com/favicon.ico 401 (Unauthorized)

thats the only error that shows. What am i doing wrong? why is my table blank?

html

<p>Please select a team name from the list to view table</p>
<form>
    <select name="users" onchange="showTeam(this.value)">
        <option value="">Select a team:</option>
        <option value="1">bobcats</option>
        <option value="2">rangers</option>
        <option value="3">hawks</option>
        <option value="4">rockets</option>
    </select>
</form>
<br>
<div id="teamInfo"><b></b></div>

JS script

// script for onchange event 
    function showTeam(str) {
        if (str == "") {
            document.getElementById("teamInfo").innerHTML = "";
            return;
        } else { 
            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("teamInfo").innerHTML = xmlhttp.responseText;
                }
            };
            xmlhttp.open("GET","phpfiles/getTeam.php?q="+str,true);
            xmlhttp.send();
        }
    }

php file

<!DOCTYPE html>
<html>
    <head>
        <style>
        table {
            width: 100%;
            border-collapse: collapse;
        }

        table, td, th {
            border: 1px solid black;
            padding: 5px;
        }

        th {text-align: left;}
        </style>
    </head>
<body>

<?php
    $q = intval($_GET['q']);

    $con = mysqli_connect('localhost','....','.....','....');
    if (!$con) {
        die('Could not connect: ' . mysqli_error($con));
    }

    mysqli_select_db($con,"ajax_demo");
    $sql="SELECT * FROM teams WHERE teamname = '".$q."'";
    $result = mysqli_query($con,$sql);

    echo "<table>
    <tr>
    <th>teamname</th>
    <th>city</th>
    <th>bestplayer</th>
    <th>yearformed</th>
    <th>website</th>
    </tr>";
    while($row = mysqli_fetch_array($result)) {
        echo "<tr>";
        echo "<td>" . $row['teamname'] . "</td>";
        echo "<td>" . $row['city'] . "</td>";
        echo "<td>" . $row['bestplayer'] . "</td>";
        echo "<td>" . $row['yearformed'] . "</td>";
        echo "<td>" . $row['website'] . "</td>";
        echo "</tr>";
    }
    echo "</table>";
    mysqli_close($con);
?>

Just an observation but shouldn`t code be like this ?

        xmlhttp.open("GET","phpfiles/getTeam.php?q="+str,true);
        xmlhttp.send();
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                document.getElementById("teamInfo").innerHTML = xmlhttp.responseText;
            }
        };

Also try to print_r($_GET) to see if the value is being posted

不确定这是否问题所在,但您使用的是intval()(例如,将输入字符串转换为整数),并且数据库中的团队名称与此不匹配(假设您给团队指定名称而不是数字)。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM