使用AJAX / PHP时为什么会收到未定义的索引错误?

时间:2016-07-05 20:08:34

标签: javascript php jquery ajax xampp

我目前正在开发远程控制应用程序,我试图将操纵杆坐标从Javascript发送到PHP。但是,由于出现Undefined Index错误,我无法通过PHP文件访问数据。

我知道这个问题已经多次得到解决,但过去一周我一直在浏览论坛和Google,而且我尝试过的解决方案都没有用过,所以如果有人能看一下,我会很感激我的代码,看我是否有一些基本的误解或特定于应用程序的问题。

编辑:我之前忘了提到这一点,但是我尝试了一些其他人在网上发布的PHP / AJAX代码示例,并且运行该代码也无法正常工作,这让我觉得这可能是我服务器的问题配置。

我的代码:(仅包含我认为与问题相关的文件;如果您想查看项目中的其他文件,请告诉我。) JavaScript和PHP文件位于同一目录中。我正在使用XAMPP。

joystick.js (包含ajax调用)

var hasGP = false;
var repGP;

// Joystick State Variables
var triggerPressed;
var x_value;
var y_value;
var z_value;
var t_value;

function canGame() 
{
    return "getGamepads" in navigator;
}

function reportOnGamepad() 
{
    // Display the gamepad's ID.
    var gp = navigator.getGamepads()[0];
    var html = "";
    html += "<u>ID</u>: " + gp.id + "<br>";

    // Display the status of the x-axis.
    x_value = gp.axes[0];
    html += "<u>x-axis</u>: " + x_value + "<br>";

    // Display the status of the y-axis.
    trigger_pressed = gp.buttons[0].pressed;
    y_value = gp.axes[1];
    if (trigger_pressed)
    {
        html += "<u>Pitch angle</u>: " + y_value + "<br>";
    }
    else
    {
        html += "<u>y-axis</u>: " + y_value + "<br>";
    }

    // Display the status of the z-axis.
    z_value = gp.axes[3];
    html += "<u>z-axis</u>: " + z_value + "<br>";

    // Display the status of the t-axis.
    t_value = gp.axes[2];
    html += "<u>Roll angle</u>: " + t_value + "<br>";

    $("#gamepadDisplay").html(html);

    $.ajax({ url: 'ajax.php',
        type: 'POST',
        data: {x:x_value},
        success: function(data) 
        {
            console.log("x_value " + data + " has been sent to the  server.");
        }
    });

}

$(document).ready(function() 
{
    if(canGame()) 
    {
        var prompt = "To begin using your gamepad, connect it and press any button!";
        $("#gamepadPrompt").text(prompt);

        $(window).on("gamepadconnected", function() 
        {
            hasGP = true;
            $("#gamepadPrompt").html("<b>The gamepad has been successfully connected.</b><br><br>");
            console.log("connection event");
            repGP = window.setInterval(reportOnGamepad,100);
        });

        $(window).on("gamepaddisconnected", function() 
        {
            console.log("disconnection event");
            $("#gamepadPrompt").text(prompt);
            window.clearInterval(repGP);
        });

        // Set up an interval for Chrome
        var checkGP = window.setInterval(function() 
        {
            console.log('checkGP');
            if(navigator.getGamepads()[0]) 
            {
                if(!hasGP) $(window).trigger("gamepadconnected");
                window.clearInterval(checkGP);
            }
        }, 500);
    }
});

ajax.php (应向其发送Javascript数据的文件)

<?php
    $temp = $_POST['x'];
    if(isset($temp) && !empty($temp))
    {
        echo "x value is ". $temp ."!"; // Success Message
    }
?>

错误讯息:

  

注意:未定义的索引:第2行的C:\ xampp \ htdocs \ TeachMoverInterface \ ajax.php中的x

PHP $ _POST数组似乎是空的。

可能有用的浏览器调试信息:

enter image description here

enter image description here

正如我所说,过去一周我一直在寻找论坛,所以我为解决问题而尝试的一些操作是在ajax方法中更改格式化,更改缓存和异步属性,替换ajax使用$ .post调用或XMLHttpRequest调用,用ajax调用中的数字替换JavaScript变量,以及我忘记的其他一些方法。

3 个答案:

答案 0 :(得分:0)

<?php
if(!empty($_POST)){
    $temp = $_POST['x'];
    if(isset($temp) && !empty($temp))
    {
        echo "x value is ". $temp ."!"; // Success Message
    }
}
?>

修改你的ajax.php代码

答案 1 :(得分:0)

正如一些人所指出的那样,我的谷歌Chrome控制台屏幕截图显示ajax.php文件正在接收ajax调用发送的数据。我的错误是我认为直接访问ajax.php文件应输出数据,但Jay Blanchard指出这是一种不正确的方法。感谢所有指出我的错误的人。另外,感谢Jeremy Harris在时间性能方面提到了一种可能更适合我应用的AJAX替代方案。

答案 2 :(得分:-1)

将数据:{x:x_value}更改为数据:{&#34; x&#34;:x_value},