php脚本无法识别由ajax传递的值

时间:2016-05-17 20:15:52

标签: javascript php mysql ajax recursion

这是ajaxhello.js函数:aFunction从ajax.php返回游戏周价值

    function aFunction(){
      var   gameweekVal = 1;/*This is a random value, which is used to make the db call during ONLOAD  */
     if (window.XMLHttpRequest) {
            xmlhttp = new XMLHttpRequest();
        } else {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        var PageToSendTo = "ajax.php?";
        var VariablePlaceholder = "gameweekVal=";
        var myVariable = gameweekVal;
        var UrlToSend = PageToSendTo + VariablePlaceholder + myVariable;
        xmlhttp.onreadystatechange = function () {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
               var dataReturn = xmlhttp.responseText;

           var ret=  document.getElementById("currentgameweek").innerHTML = dataReturn; 
           getFixtures(dataReturn); /*The dataReturn vale is passed to the getFixtures(aVal) This value should then be passed to ajax2.php and used to as a select value in the SQL statement. It's not working, why ? */
            }
        };
        xmlhttp.open("GET", UrlToSend, true);
        xmlhttp.send();

    }

    function getFixtures(aVal){


       var  gameweekVal = aVal;
     if (window.XMLHttpRequest) {
            xmlhttp = new XMLHttpRequest();
        } else {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        var PageToSendTo = "ajax2.php?";
        var VariablePlaceholder = "gameweekVal=";
        var myVariable = gameweekVal;
        var UrlToSend = PageToSendTo + VariablePlaceholder + myVariable;
        xmlhttp.onreadystatechange = function () {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
               var dataReturn = xmlhttp.responseText;

            document.getElementById("ajaxfixtures").innerHTML = dataReturn; 

            }
        };
        xmlhttp.open("POST", UrlToSend, true);
        xmlhttp.send();

    }

    /******************************************************/

        enter code here<!DOCTYPE html>
    <!--
    To change this license header, choose License Headers in Project Properties.
    To change this template file, choose Tools | Templates
    and open the template in the editor.
    -->
    <html>
        <head>
            <meta charset="UTF-8">
            <title></title>
        </head>
        <body>
            <?php
            include 'configuration.php';
            include 'connectTodb.php';
         /*   include 'dataformat.php'; */

            $sqli_query= " SELECT `gameweek` FROM `gameweeks` WHERE now() BETWEEN `start`AND `end`";


            $result= mysqli_query($connection,$sqli_query);

            while($row= mysqli_fetch_array($result))
            {
                print($row['gameweek']);
            }
            ?>
        </body>
    </html>

    /****************************************************************/


    <!DOCTYPE html>
    <!--
    To change this license header, choose License Headers in Project Properties.
    To change this template file, choose Tools | Templates
    and open the template in the editor.
    -->
    <html>
        <head>
            <meta charset="UTF-8">
            <title></title>
        </head>
        <body>
            <?php
            include 'configuration.php';
            include 'connectTodb.php';
         /*   include 'dataformat.php'; */

            $sqli_query= " SELECT `gameweek` FROM `gameweeks` WHERE now() BETWEEN `start`AND `end`";


            $result= mysqli_query($connection,$sqli_query);

            while($row= mysqli_fetch_array($result))
            {
                print($row['gameweek']);
            }
            ?>
        </body>
    </html>


<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <?php
        include 'configuration.php';
        include 'connectTodb.php';
        $gameweekValIn = $_REQUEST['gameweekVal'];/* The value is being passed when gameweekVal is enclosed in double quotes */

         print("<table  border='1'>");

      $sqli_query =   "SELECT `gameweek`,`home_team_id`,`home_team`, `away_team` FROM `fixtures` WHERE `gameweek`=".$gameweekValIn;
        $result= mysqli_query($connection,$sqli_query);

       print("<tr><th></th><th>$hash_symbol</th><th>$home_teamColHeading</th><th>$white_space</th><th>$white_space</th><th>$white_space</th><th>$away_teamColHeading</th><th>$white_space</th><th>$white_space</th><th>$white_space</th></tr>");

        while($row= mysqli_fetch_array($result))
        {

        print("<tr><td ".$row['gameweek']."></td><td>".$row['home_team_id']."</td>"."<td>".$row['home_team']."</td>"."<td>$Letter_P</td>"."<td>$Letter_S</td>"."<td>$Letter_H</td>"."<td>".$row['away_team']."</td>"."<td>$Letter_P</td>"."<td>$Letter_S</td>"."<td>$Letter_H</td>"."</tr>");  

        }

        print("</table>"); 

    mysqli_close($connection);
        ?>
    </body>
</html>

我已经包含了ajaxhello.js,第一个函数aFunction()用于从ajax.php获取gameweek值,这个值被传递给getFixtures(aVal),它应该能够用作选择器ajax2.php

1 个答案:

答案 0 :(得分:0)

因为您通过Ajax使用GET。你的url参数应该是这样的;

 Xmlhttp.open('GET', 'ajax2.php?variable='+answer+'&varible2='+answer2+',true);
 Xmlhttp.send();

对于POST

 Xmlhttp.open('POST', 'ajax2.php', true);
 Xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
 Xmlhttp.send('variable='+answer);

希望这能清楚地说明你可能出错的地方

在你的php中,你可以得到已解析的内容:

GET

If(isset($_GET['variable'])){}

对于POST

If(isset($_POST['variable'])){}