多个Ajax调用 - 第二个Ajax函数不显示PHP结果

时间:2014-10-18 22:22:44

标签: javascript php ajax

我试图获得2个不同的Ajax结果,以便在我构建的浏览器游戏中动态显示。我有PHP文件和正确的div设置。当我运行此脚本时,它只显示其中一个值。我需要dayofweek和hourofday才能显示在同一个网页上。

Javascript只有没有Jquery - Ajax - 无法开始工作所以Jquery在底部回答了问题!

var xmlHttp = createXmlHttpRequestObject();
var xmlHttp2 = createXmlHttpRequestObject();

function createXmlHttpRequestObject(){
var xmlHttp;

if(window.ActiveXObject){
    try{
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }catch(e){
        xmlHttp = false;

    }
}else{
    try{
        xmlHttp = new XMLHttpRequest();
    }catch(e){
        xmlHttp = false;
    }
}
if(!xmlHttp)
    alert("cant create that object hoss!");
else
    return xmlHttp;
}

function hourofday(){

if(xmlHttp.readyState==0 || xmlHttp.readyState==4) {

    hourofday = encodeURIComponent(document.getElementById("hourofday").value);
    xmlHttp.open("POST", "hourofday.php", true);
    xmlHttp.onreadystatechange = handleServerResponse;
    xmlHttp.send(null);
    setTimeout('hourofday()',1000);

    }

}


function dayofweek(){

if(xmlHttp2.readyState==0 || xmlHttp2.readyState==4) {

    dayofweek = encodeURIComponent(document.getElementById("dayofweek").value);
    xmlHttp2.open("POST", "dayofweek.php", true);
    xmlHttp2.onreadystatechange = handleServerResponse2;
    xmlHttp2.send(null);
    setTimeout('dayofweek()',1000);

    }

}

function handleServerResponse(){
if(xmlHttp.readyState==4){
    if(xmlHttp.status==200){
        xmlResponse = xmlHttp.responseXML;
        xmlDocumentElement = xmlResponse.documentElement;
        time = xmlDocumentElement.firstChild.data;
        document.getElementById("hourofday").innerHTML = time;




    }else{
        alert('Something went wrong.');
    }
}
}
function handleServerResponse2(){
if(xmlHttp2.readyState==4){
    if(xmlHttp2.status==200){
        xmlResponse = xmlHttp2.responseXML;
        xmlDocumentElement = xmlResponse.documentElement;
        day = xmlDocumentElement.firstChild.data;
        document.getElementById("dayofweek").innerHTML = day;




    }else{
        alert('Something went wrong.');
    }
    }
}

dayofweek.php:

$dayofyear_query = "SELECT `dayofyear` FROM `gforce1`.`players` WHERE `id`='1'" or die(mysql_error);
$dayofyear_result = mysql_query($dayofyear_query);
$dayofyear_row = mysql_fetch_row($dayofyear_result);
$dayofyear = $dayofyear_row[0];

$dayinyear = $dayofyear;

if (
$dayinyear ==1||
$dayinyear ==8||
$dayinyear ==15||
$dayinyear ==22||
$dayinyear ==29||
$dayinyear ==36||
$dayinyear ==43||
$dayinyear ==50||
$dayinyear ==57||
$dayinyear ==64||
$dayinyear ==71||
$dayinyear ==78||
$dayinyear ==85||
$dayinyear ==92||
$dayinyear ==99||
$dayinyear ==106||
$dayinyear ==113||
$dayinyear ==120||
$dayinyear ==127||
$dayinyear ==134||
$dayinyear ==141||
$dayinyear ==148||
$dayinyear ==155||
$dayinyear ==162||
$dayinyear ==169||
$dayinyear ==176||
$dayinyear ==183||
$dayinyear ==190||
$dayinyear ==197||
$dayinyear ==204||
$dayinyear ==211||
$dayinyear ==218||
$dayinyear ==225||
$dayinyear ==232||
$dayinyear ==239||
$dayinyear ==246||
$dayinyear ==253||
$dayinyear ==260||
$dayinyear ==267||
$dayinyear ==274||
$dayinyear ==281||
$dayinyear ==288||
$dayinyear ==295||
$dayinyear ==302||
$dayinyear ==309||
$dayinyear ==316||
$dayinyear ==323||
$dayinyear ==330||
$dayinyear ==337||
$dayinyear ==344||
$dayinyear ==351||
$dayinyear ==358){
    $weekday = 1;
}

//tuesday
if (
$dayinyear ==   2   ||
$dayinyear ==   9   ||
$dayinyear ==   16  ||
$dayinyear ==   23  ||
$dayinyear ==   30  ||
$dayinyear ==   37  ||
$dayinyear ==   44  ||
$dayinyear ==   51  ||
$dayinyear ==   58  ||
$dayinyear ==   65  ||
$dayinyear ==   72  ||
$dayinyear ==   79  ||
$dayinyear ==   86  ||
$dayinyear ==   93  ||
$dayinyear ==   100 ||
$dayinyear ==   107 ||
$dayinyear ==   114 ||
$dayinyear ==   121 ||
$dayinyear ==   128 ||
$dayinyear ==   135 ||
$dayinyear ==   142 ||
$dayinyear ==   149 ||
$dayinyear ==   156 ||
$dayinyear ==   163 ||
$dayinyear ==   170 ||
$dayinyear ==   177 ||
$dayinyear ==   184 ||
$dayinyear ==   191 ||
$dayinyear ==   198 ||
$dayinyear ==   205 ||
$dayinyear ==   212 ||
$dayinyear ==   219 ||
$dayinyear ==   226 ||
$dayinyear ==   233 ||
$dayinyear ==   240 ||
$dayinyear ==   247 ||
$dayinyear ==   254 ||
$dayinyear ==   261 ||
$dayinyear ==   268 ||
$dayinyear ==   275 ||
$dayinyear ==   282 ||
$dayinyear ==   289 ||
$dayinyear ==   296 ||
$dayinyear ==   303 ||
$dayinyear ==   310 ||
$dayinyear ==   317 ||
$dayinyear ==   324 ||
$dayinyear ==   331 ||
$dayinyear ==   338 ||
$dayinyear ==   345 ||
$dayinyear ==   352 ||
$dayinyear ==   359 ){
    $weekday = 2;
}

//wednesday
if (
$dayinyear ==   3   ||
$dayinyear ==   10  ||
$dayinyear ==   17  ||
$dayinyear ==   24  ||
$dayinyear ==   31  ||
$dayinyear ==   38  ||
$dayinyear ==   45  ||
$dayinyear ==   52  ||
$dayinyear ==   59  ||
$dayinyear ==   66  ||
$dayinyear ==   73  ||
$dayinyear ==   80  ||
$dayinyear ==   87  ||
$dayinyear ==   94  ||
$dayinyear ==   101 ||
$dayinyear ==   108 ||
$dayinyear ==   115 ||
$dayinyear ==   122 ||
$dayinyear ==   129 ||
$dayinyear ==   136 ||
$dayinyear ==   143 ||
$dayinyear ==   150 ||
$dayinyear ==   157 ||
$dayinyear ==   164 ||
$dayinyear ==   171 ||
$dayinyear ==   178 ||
$dayinyear ==   185 ||
$dayinyear ==   192 ||
$dayinyear ==   199 ||
$dayinyear ==   206 ||
$dayinyear ==   213 ||
$dayinyear ==   220 ||
$dayinyear ==   227 ||
$dayinyear ==   234 ||
$dayinyear ==   241 ||
$dayinyear ==   248 ||
$dayinyear ==   255 ||
$dayinyear ==   262 ||
$dayinyear ==   269 ||
$dayinyear ==   276 ||
$dayinyear ==   283 ||
$dayinyear ==   290 ||
$dayinyear ==   297 ||
$dayinyear ==   304 ||
$dayinyear ==   311 ||
$dayinyear ==   318 ||
$dayinyear ==   325 ||
$dayinyear ==   332 ||
$dayinyear ==   339 ||
$dayinyear ==   346 ||
$dayinyear ==   353 ||
$dayinyear ==   360){
    $weekday = 3;
}
// Thursday
if(
$dayinyear ==   4   ||
$dayinyear ==   11  ||
$dayinyear ==   18  ||
$dayinyear ==   25  ||
$dayinyear ==   32  ||
$dayinyear ==   39  ||
$dayinyear ==   46  ||
$dayinyear ==   53  ||
$dayinyear ==   60  ||
$dayinyear ==   67  ||
$dayinyear ==   74  ||
$dayinyear ==   81  ||
$dayinyear ==   88  ||
$dayinyear ==   95  ||
$dayinyear ==   102 ||
$dayinyear ==   109 ||
$dayinyear ==   116 ||
$dayinyear ==   123 ||
$dayinyear ==   130 ||
$dayinyear ==   137 ||
$dayinyear ==   144 ||
$dayinyear ==   151 ||
$dayinyear ==   158 ||
$dayinyear ==   165 ||
$dayinyear ==   172 ||
$dayinyear ==   179 ||
$dayinyear ==   186 ||
$dayinyear ==   193 ||
$dayinyear ==   200 ||
$dayinyear ==   207 ||
$dayinyear ==   214 ||
$dayinyear ==   221 ||

$dayinyear ==   228 ||
$dayinyear ==   235 ||
$dayinyear ==   242 ||
$dayinyear ==   249 ||
$dayinyear ==   256 ||
$dayinyear ==   263 ||
$dayinyear ==   270 ||
$dayinyear ==   277 ||
$dayinyear ==   284 ||
$dayinyear ==   291 ||
$dayinyear ==   298 ||
$dayinyear ==   305 ||
$dayinyear ==   312 ||
$dayinyear ==   319 ||
$dayinyear ==   326 ||
$dayinyear ==   333 ||
$dayinyear ==   340 ||
$dayinyear ==   347 ||
$dayinyear ==   354 ||
$dayinyear ==   361){
    $weekday = 4;
}
//friday
if(
$dayinyear ==   5   ||
$dayinyear ==   12  ||
$dayinyear ==   19  ||
$dayinyear ==   26  ||
$dayinyear ==   33  ||
$dayinyear ==   40  ||
$dayinyear ==   47  ||
$dayinyear ==   54  ||
$dayinyear ==   61  ||
$dayinyear ==   68  ||
$dayinyear ==   75  ||
$dayinyear ==   82  ||
$dayinyear ==   89  ||
$dayinyear ==   96  ||
$dayinyear ==   103 ||
$dayinyear ==   110 ||
$dayinyear ==   117 ||
$dayinyear ==   124 ||
$dayinyear ==   131 ||
$dayinyear ==   138 ||
$dayinyear ==   145 ||
$dayinyear ==   152 ||
$dayinyear ==   159 ||
$dayinyear ==   166 ||
$dayinyear ==   173 ||
$dayinyear ==   180 ||
$dayinyear ==   187 ||
$dayinyear ==   194 ||
$dayinyear ==   201 ||
$dayinyear ==   208 ||
$dayinyear ==   215 ||
$dayinyear ==   222 ||
$dayinyear ==   229 ||
$dayinyear ==   236 ||
$dayinyear ==   243 ||
$dayinyear ==   250 ||
$dayinyear ==   257 ||
$dayinyear ==   264 ||
$dayinyear ==   271 ||
$dayinyear ==   278 ||
$dayinyear ==   285 ||
$dayinyear ==   292 ||
$dayinyear ==   299 ||
$dayinyear ==   306 ||
$dayinyear ==   313 ||
$dayinyear ==   320 ||
$dayinyear ==   327 ||
$dayinyear ==   334 ||
$dayinyear ==   341 ||
$dayinyear ==   348 ||
$dayinyear ==   355 ||
$dayinyear ==   362){
    $weekday = 5;
}
//saturday
if(
$dayinyear ==   6   ||
$dayinyear ==   13  ||
$dayinyear ==   20  ||
$dayinyear ==   27  ||
$dayinyear ==   34  ||
$dayinyear ==   41  ||
$dayinyear ==   48  ||
$dayinyear ==   55  ||
$dayinyear ==   62  ||
$dayinyear ==   69  ||
$dayinyear ==   76  ||
$dayinyear ==   83  ||
$dayinyear ==   90  ||
$dayinyear ==   97  ||
$dayinyear ==   104 ||
$dayinyear ==   111 ||
$dayinyear ==   118 ||
$dayinyear ==   125 ||
$dayinyear ==   132 ||
$dayinyear ==   139 ||
$dayinyear ==   146 ||
$dayinyear ==   153 ||
$dayinyear ==   160 ||
$dayinyear ==   167 ||
$dayinyear ==   174 ||
$dayinyear ==   181 ||
$dayinyear ==   188 ||
$dayinyear ==   195 ||
$dayinyear ==   202 ||
$dayinyear ==   209 ||
$dayinyear ==   216 ||
$dayinyear ==   223 ||
$dayinyear ==   230 ||
$dayinyear ==   237 ||
$dayinyear ==   244 ||
$dayinyear ==   251 ||
$dayinyear ==   258 ||
$dayinyear ==   265 ||
$dayinyear ==   272 ||
$dayinyear ==   279 ||
$dayinyear ==   286 ||
$dayinyear ==   293 ||
$dayinyear ==   300 ||
$dayinyear ==   307 ||
$dayinyear ==   314 ||
$dayinyear ==   321 ||
$dayinyear ==   328 ||
$dayinyear ==   335 ||
$dayinyear ==   342 ||
$dayinyear ==   349 ||
$dayinyear ==   356 ||
$dayinyear ==   363 ){
    $weekday = 6;
}
//sunday

if(
$dayinyear ==   7   ||
$dayinyear ==   14  ||
$dayinyear ==   21  ||
$dayinyear ==   28  ||
$dayinyear ==   35  ||
$dayinyear ==   42  ||
$dayinyear ==   49  ||
$dayinyear ==   56  ||
$dayinyear ==   63  ||
$dayinyear ==   70  ||
$dayinyear ==   77  ||
$dayinyear ==   84  ||
$dayinyear ==   91  ||
$dayinyear ==   98  ||
$dayinyear ==   105 ||
$dayinyear ==   112 ||
$dayinyear ==   119 ||
$dayinyear ==   126 ||
$dayinyear ==   133 ||
$dayinyear ==   140 ||
$dayinyear ==   147 ||
$dayinyear ==   154 ||
$dayinyear ==   161 ||
$dayinyear ==   168 ||
$dayinyear ==   175 ||
$dayinyear ==   182 ||
$dayinyear ==   189 ||
$dayinyear ==   196 ||
$dayinyear ==   203 ||
$dayinyear ==   210 ||
$dayinyear ==   217 ||
$dayinyear ==   224 ||
$dayinyear ==   231 ||
$dayinyear ==   238 ||
$dayinyear ==   245 ||
$dayinyear ==   252 ||
$dayinyear ==   259 ||
$dayinyear ==   266 ||
$dayinyear ==   273 ||
$dayinyear ==   280 ||
$dayinyear ==   287 ||
$dayinyear ==   294 ||
$dayinyear ==   301 ||
$dayinyear ==   308 ||
$dayinyear ==   315 ||
$dayinyear ==   322 ||
$dayinyear ==   329 ||
$dayinyear ==   336 ||
$dayinyear ==   343 ||
$dayinyear ==   350 ||
$dayinyear ==   357 ||
$dayinyear ==   364 ){
    $weekday = 7;
}
header('Content-Type: text/xml');
echo '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>';
echo '<response>';      

    if ($weekday == 1){
        echo 'Monday';
    }
    if ($weekday == 2){
        echo 'Tuesday';
    }
    if ($weekday == 3){
        echo 'Wednesday';
    }
    if ($weekday == 4){
        echo 'Thursday';
    }
    if ($weekday == 5){
        echo 'Friday';
    }
    if ($weekday == 6){
        echo 'Saturday';
    }
    if ($weekday == 7){
        echo 'Sunday';
    }
echo '</response>';
?>

hourofday.php:

header('Content-Type: text/xml');
echo '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>';
echo '<response>';

    $hourofday_query = "SELECT `hourofday` FROM `gforce1`.`players` WHERE `id`='1'" or die(mysql_error);
        $hourofday_result = mysql_query($hourofday_query);
        $hourofday_row = mysql_fetch_row($hourofday_result);
        $hourofday = $hourofday_row[0];


if ($hourofday >= 12){
    $ampm = 'PM';
}
if ($hourofday < 12){
    $ampm = 'AM';
}
if ($hourofday >= 25){
    $hourofday = 1;
}


echo $hourofday.':00 '.$ampm;
echo '</response>';

2 个答案:

答案 0 :(得分:0)

这是一些小代码重构。我这里只做一个ajax调用:

此文件名为time.php:

<?php
header('Content-Type: text/xml');
echo '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>';
echo '<response>';

    $time_query = "SELECT `dayofyear`, `hourofday`  FROM `gforce1`.`players` WHERE `id`='1'" or die(mysql_error);
    $time_result = mysql_query($time_query);
    $time_row = mysql_fetch_row($time_result);
    $dayofyear = $time_row[0];
    $hourofday = $time_row[1];

    // Here lies all your logic to get the $dayofyear value
    // Here lies all your logic to get the $hourofday value

    echo '<dayofweek>' . $dayofyear . '</dayofweek>';
    echo '<hourofday>' . $hourofday . '</hourofday>';
    echo '</response>';
?>

这是javascript:

<script>
    var xmlHttp = createXmlHttpRequestObject();

    function createXmlHttpRequestObject() {
        var xmlHttp;

        if (window.ActiveXObject) {
            try {
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {
                xmlHttp = false;

            }
        } else {
            try {
                xmlHttp = new XMLHttpRequest();
            } catch (e) {
                xmlHttp = false;
            }
        }
        if (!xmlHttp)
            alert("cant create that object hoss!");
        else
            return xmlHttp;
    }

    function timeCurrent() {

        if (xmlHttp.readyState == 0 || xmlHttp.readyState == 4) {

           // hourofday = encodeURIComponent(document.getElementById("hourofday").value);
            xmlHttp.open("POST", "time.php", true);
            xmlHttp.onreadystatechange = handleServerResponse;
            xmlHttp.send(null);
        }

    }

    function handleServerResponse() {
        if (xmlHttp.readyState == 4) {
            if (xmlHttp.status == 200) {
                xmlResponse = xmlHttp.responseXML;
                xmlDocumentElement = xmlResponse.documentElement;
                var dayofweek = xmlDocumentElement.getElementsByTagName("dayofweek")[0].textContent;
                var hourofday = xmlDocumentElement.getElementsByTagName("hourofday")[0].textContent;
                document.getElementById("dayofweek").innerHTML = dayofweek;
                document.getElementById("hourofday").innerHTML = hourofday;


            } else {
                alert('Something went wrong.');
            }
        }
    }
    setInterval(timeCurrent, 1000);
</script>

由于数据库的原因,我无法使用您的配置尝试代码,但它对我来说是假值。

答案 1 :(得分:0)

好的家伙,所以我学会了如何jQuery相同的响应,这是完美的工作!!

jQuery Ajax:

function dayofweek(){
    $.ajax({
        url: "dayofweek.php",
        type: "POST",
        dataType: "xml",
        success: function (xml){
        var day = $(xml).find('day').first().text();
        $("#dayofweek").html(day);

    },
         error: function (xhr, status) {
         alert("Sorry, there was a problem!");
    },
         complete: function (xhr, status) {
    }
 });
}

function hourofday(){
    $.ajax({
        url: "hourofday.php",
        type: "POST",
        dataType: "xml",
        success: function (xml){
        var response = $(xml).find('response').first().text();
        $("#hourofday").html(response);
    },
        error: function (xhr, status) {
        alert("Sorry, there was a problem!");
  },
        complete: function (xhr, status) {
   }

 });

}