试图从ajax返回php数据

Trying to return php data from ajax

本文关键字:php 数据 返回 ajax      更新时间:2023-09-26

我对Ajax有点陌生,我一直在努力弄清楚我做错了什么。我从数据库中提取结果并将其放入xml中。当我通过xml循环时,我正在尝试执行一个php文件,同时从xml结果发送ID号,然后从php文件返回"echo"。我不确定我是完全错了还是只是少了一个部分,但结果返回"未定义"。

这是php文件,我试图得到回显和显示。

echo rating_bar($id);
function rating_bar($id) {
    //other code, but $static_rater is what gets echoed
    $static_rater = "";
    $static_rater .= '<div id="ratingblock" class="ratingblock">';
    $static_rater .= '<div id="unit_long'.$id.'">';
    $static_rater .= '<ul id="unit_ul'.$id.'" class="unit-rating" style="width:'.$rating_unitwidth*$units.'px;">';
    $static_rater .= '<li class="current-rating" style="width:'.$rating_width.'px;"></li>';
    $static_rater .= '</ul>';
    $static_rater .= '<p class="static">Rating: <strong> '.$rating1.'</strong>/'.$units.' ('.$count.' '.$tense.' cast)</p>';
    $static_rater .= '</div>';
    $static_rater .= '</div>';
    //return join("'n", $static_rater);
    echo $static_rater;exit;
}

这是。js代码,我试图得到返回结果。

downloadUrl("phpsqlajax_genxml.php", function(data) {
    var xml = data.responseXML; 
    var bounds = new google.maps.LatLngBounds();
    var markers = xml.documentElement.getElementsByTagName("marker"); 
    // alert("downloadUrl callback, length="+markers.length);
    for (var i = 0; i < markers.length; i++)  { 
        var id = markers[i].getAttribute("id");
        if (!id) id = "id "+i;
        var name = markers[i].getAttribute("name");
        if (!name) name = "name "+i;
        var address = markers[i].getAttribute("address"); 
        if (!address) address = "address";
        var citystate = markers[i].getAttribute("citystate");
        if (!citystate) citystate = "city, ST";
        var phone = markers[i].getAttribute("phone");
        if (!phone) phone = "phone number";
        var type = markers[i].getAttribute("type");
        var point = new google.maps.LatLng( 
          parseFloat(markers[i].getAttribute("lat")), 
          parseFloat(markers[i].getAttribute("lng"))); 
        bounds.extend(point);
        var html = "<b>" + name + "</b> <br/>" + address + "<br/>" + citystate + "<br/>" + phone;  //html inside InfoWindow 
        var url = "starrating/_drawrating.php?id=" + id + "";
        //var contentString = ajaxLoad(url, parseResults, true);
        //var contentString =   downloadUrl(url, "POST", "text=" + text, completed);
        var contentString = AJAX('starrating/_drawrating.php','id='+id,
            function(data) {
                var htm = $("#ratingblock").html(data);
                alert(htm);
           }
      );
        var description = "<br><br>description" + id + " <br><b>" + name + "</b> <br/>" + address + "<br/>" + citystate + "<br/>" + phone;  //html inside InfoWindow
        var icon = customIcons[type] || {}; 
        var marker = new google.maps.Marker({ 
            map: map, 
            position: point, 
            icon: icon.icon, 
            shadow: icon.shadow,
            animation: google.maps.Animation.DROP
        }); 
        bindInfoWindow(marker, map, infoBubble, html, description, contentString); 
    } 
}); 

function AJAX(url, data, callback)
{
var xmlhttp;
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)
    {
        callback(xmlhttp.responseText);
    }
}
xmlhttp.open("POST",url,true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send(data);

}

编辑:好的,所以我一直在玩这个和更新我的代码上面。现在,当我使用firebug运行这个程序时,我可以看到帖子和响应是这样的:

<div id="ratingblock" class="ratingblock"><div id="unit_long10"><ul id="unit_ul10" class="unit-rating" style="width:150px;"><li class="current-rating" style="width:0px;"></li></ul><p class="static">Rating: <strong> 0.0</strong>/5 (0 votes cast)</p></div></div>

但是警报只显示[object object]而信息窗口显示[object object]。所以我知道它的调用和返回数据,我搜索并尝试了我能想到的一切,以使上面的部分正确地出现在信息窗口内。任何想法吗?

编辑# 2

我在尝试下面的新方法。

var contentString = $.ajax({
                            type:"POST",
                            url: "starrating/_drawrating.php",
                            dataType: "html",
                            data:"id="+id,
                            success: function(data){
                                    var $response=$(data);
                                    $response.find('ratingblock').html();
                                    console.log($response);
                                }
                        });

控制台返回"Object[div#ratingblock]"。ratingblock]",但结果仍然是[object object]。你知道我错过了什么吗?

AJAX中的A代表异步,这意味着JS不会等待PHP数据返回。因此,您不能简单地将AJAX调用的结果分配给变量,您必须注册一个函数,该函数将在某些数据返回时调用。这就是当前空的function(result) {}回调的作用。

这有点像让别人去拿东西,而不是站在原地等他们回来。这个回调函数,在这个稍微有点模糊的类比中,是一个记录,当它们返回时,您打算做什么。

您在url中发送部分数据,在post body中发送部分数据,将这两部分都放在post body中。例如

AJAX('starrating/_drawrating.php','id='+id,

$static_rater是一个数组,你不能对它使用连接操作符。

$static_rater[] = "'n".'<div class="ratingblock">';
$static_rater[] = '<div id="unit_long'.$id.'">';
$static_rater[] = '<ul id="unit_ul'.$id.'" class="unit-rating" style="width:'.$rating_unitwidth*$units.'px;">';
$static_rater[] = '<li class="current-rating" style="width:'.$rating_width.'px;">Currently '.$rating2.'/'.$units.'</li>';
$static_rater[] = '</ul>';
$static_rater[] = '<p class="static">'.$id.'. Rating: <strong> '.$rating1.'</strong>/'.$units.' ('.$count.' '.$tense.' cast)</p>';
$static_rater[] = '</div>';
$static_rater[] = '</div>'."'n'n";