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 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化