AJAX不会发回

时间:2014-03-16 18:36:57

标签: javascript php ajax

首先,我想说我是Ajax的初学者。我只是个初学者。我希望你能帮助我。

我想向ajax脚本发送一个id,然后我运行一个查询。然后我想返回页面的输出。我有一个页面quick_view_page.js我设置了id并将其发送到ajax_project_info.php,我通过链接检索id。如果我在链接中打开ajax_project_info.php并设置变量id,则效果很好。

我无法将ajax_project_info的输出返回给quick_view_page.js。

我的quick_view_page.js。

//Quick view show and hide
    $('a#quick_view').click(function() {
        var projectId = $(this).find($(".qv_id")).attr('id');
        //alert(projectId);

        xmlhttp = new XMLHttpRequest();
        xmlhttp.open("GET","ajax/ajax_project_info.php?projectId=" + projectId);
        xmlhttp.send();

        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
            document.getElementByClassName("qvp_bg").innerHTML=xmlhttp.responseText;
        }

我的ajax_project_info.php

<?php
require_once("../pages/db.php");
error_reporting(E_ALL);
ini_set('display_errors', 1);
ob_start();

$projectId = $_GET["projectId"];

$query_getinfo = "SELECT * FROM tblProjecten WHERE id = '".$projectId."'";

$result_query_getinfo = mysqli_query($conn,$query_getinfo);

while ($row = mysqli_fetch_array($result_query_getinfo)) {
    $response = $row['projectnaam'];
};
else {
    $response = "Geen info beschikbaar";
}
return $response;

&GT;

谢谢!

更新: 问题似乎在“if(xmlhttp.readyState == 4&amp;&amp; xmlhttp.status == 200)”中 当我在此之间设置警报时,警报不会弹出。

更新2: 我现在有 quick_view_search.js:

        //Quick view show and hide
    $('a#quick_view').click(function() {
        var projectId = $(this).find($(".qv_id")).attr('id');


        //xmlhttp request
        xmlhttp.onreadystatechange=function(){
            if (xmlhttp.readyState==4 && xmlhttp.status==200){
                document.getElementsByClassName("qvp_bg").innerHTML=xmlhttp.responseText;
            }
        }

        xmlhttp.open("GET","ajax/ajax_project_info.php?projectId=" + projectId, true);
        xmlhttp.send();

        $.ajax({
            'url' : 'ajax/ajax_project_info.php',
            'type' : 'GET',
            'data' : {
            'projectId' : projectId
        },
        'success' : function(data) {
            document.getElementsByClassName("qvp_bg").innerHTML=data;
        }
        });


        $('.quick_view_project').css('display', 'block').hide().fadeIn();
        return false;
    });

ajax_project_info.php:

<?php
/*
require_once("../pages/db.php");
error_reporting(E_ALL);
ini_set('display_errors', 1);
ob_start();

$projectId = $_GET["projectId"];

if ($projectId) {
    $query_getinfo = "SELECT * FROM tblProjecten WHERE id = '".$projectId."'";
    $result_query_getinfo = mysqli_query($conn,$query_getinfo);

    while ($row = mysqli_fetch_array($result_query_getinfo)) {
        $response = $row['projectnaam'];
    }
    else {
        $response = "Geen info beschikbaar";
    }
}
else {
    $response = "Geen info beschikbaar";
}
echo $response;
*/
echo "test";

&GT;

它给出了以下错误:“xmlhttp未定义”。

1 个答案:

答案 0 :(得分:4)

使用echo $response代替return $response

也试试,

xmlhttp.onreadystatechange=function(){
    if (xmlhttp.readyState==4 && xmlhttp.status==200){
        document.getElementByClassName("qvp_bg").innerHTML=xmlhttp.responseText;
    }
}
xmlhttp.open("GET","ajax/ajax_project_info.php?projectId=" + projectId, true);
xmlhttp.send();

使用jQuery

将jQuery包含在项目中

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.0/jquery.min.js"></script>

获取请求

$('.myButton').click(function() {
  $.ajax({
    'url' : 'ajax/ajax_project_info.php',
    'type' : 'GET',
    'data' : {
      'projectId' : projectId
    },
    'success' : function(data) {
      document.getElementByClassName("qvp_bg").innerHTML=data;
    }
  });
});

希望这会有所帮助: - )