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 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog