dqlhsm9820 2015-08-31 10:21
浏览 99
已采纳

XMLHTTPRequest不将数据发送到PHP文件

I am using xmlhttp request to send data to my php file and then use it to get data out of a database and then show that response back at my original page. Following is the code:

<script>

 function func2(str) {
if (str == "") {
    document.getElementById("replaceContent").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("replaceContent").innerHTML = xmlhttp.responseText;
        }
    } 
    xmlhttp.open("GET","projectdata.php?q="+str,true);
    xmlhttp.send();
}
}

                    
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>

<body>
                    <div id="replaceContent">
                        This should be replaced.
                        <button onclick="func2(10)"> Click ME </button>
                    </div>
                    
</body>
</html>

and the following is the projectData.php:

<?php
    include 'connEst.php';
    // get the q parameter from URL
    $q = $_GET['id'];
    echo $q;
    $sql="SELECT * FROM projects WHERE ID = '".$q."'";
    $result = mysqli_query($conn,$sql);
    
    echo "<table>
    <tr>
    <th> $q </th>
    </tr>
    <tr>
    <th>Project Name</th>
    <th>Project Location</th>
    <th>Project Value</th>
    <th>Client Name</th>
    <th>Work Done in Project</th>
    <th>Project Summary</th>
    <th>Start Date</th>
    <th>End Date</th>
    </tr>";
    while($row = mysqli_fetch_array($result)) {
        echo "<tr>";
        echo "<td>" . $row['p_name'] . "</td>";
        echo "<td>" . $row['p_loc'] . "</td>";
        echo "<td>" . $row['p_val'] . "</td>";
        echo "<td>" . $row['client_name'] . "</td>";
        echo "<td>" . $row['p_wdone'] . "</td>";
        echo "<td>" . $row['p_summary'] . "</td>";
        echo "<td>" . $row['start_date'] . "</td>";
        echo "<td>" . $row['end_date'] . "</td>";
        echo "</tr>";
    }
    echo "</table>";
    mysqli_close($conn);
?>

I am running it on my mac using an apache server installation. I am testing this in Chrome. The $q variable I have setup is returning a blank value when I run the file.

please help!

</div>
  • 写回答

1条回答 默认 最新

  • douzhaobo6488 2015-08-31 11:14
    关注

    Your Ajax function sends a parameter "q" not "id" and the PHP looks for $_GET['id']

    In projectData.php, change the existing to:

    $q = $_GET['q'];
    

    Or change your javascript to:

    xmlhttp.open("GET","projectdata.php?id="+str,true);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)