ajax请求$ _get无效

时间:2013-11-05 20:39:11

标签: javascript php ajax

我在使用jquery在php中发送get请求时遇到问题。它只是无法检测到获取值 这是我的代码:

 function getopmerking($id) {
            document.getElementById("popup_box").style.display = "block";
            $httpreq = new XMLHttpRequest();
            $httpreq.onreadystatechange = function () {
                document.getElementById("txtouders").innerText = $httpreq.responseText;
                document.getElementById("requesteddata").innerText = $id;
            }

            $httpreq.open("GET", "files/request.php?q=1",true);
            $httpreq.send();

        }

和我的PHP代码:

    $id = $_GET["q"];
fetchData();
function fetchData()
{
    $drow = mysql_fetch_array(mysql_query("SELECT * FROM  `tblreservering` where fldllnid=$id;"));
    if(!empty($drow))
    {
        $drow['fldopmerking'];
    }else{
        echo "id is: ".$id."geen gegevens gevonden!"; 
        // i only get "id is: geen gegevens gevonden!" as output since $id is nothing.
    }   
}

1 个答案:

答案 0 :(得分:0)

如果你想在JQuery中这样做,它将如下所示:

JQuery的:

function getopmerking($id) {
  $("#popup_box").show();
  $.get("files/request.php?q=1", function(resp){
    $("#txttouders").text(resp);
    $("#requesteddata").text($id);
  });
}

PHP:

$id = $_GET["q"];
fetchData($id);
function fetchData($getId){
  $query = sprintf("SELECT * FROM  `tblreservering` where fldllnid=%d;", mysql_real_escape_string($getId));
    $drow = mysql_fetch_array(mysql_query($query));
    if(!empty($drow))
    {
        $drow['fldopmerking'];
    }else{
        echo "id is: ".$getId."geen gegevens gevonden!"; 
        // i only get "id is: geen gegevens gevonden!" as output since $id is nothing.
    }   
}