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

时间:2022-10-17 11:20:02

I am currently working on a telecontrol application in which I am trying to send joystick coordinates from Javascript to PHP. However, I am never able to access the data via the PHP file as I get an Undefined Index error.

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

I am aware that this question has been addressed many times, but I have spent the past week going through forums and Google and none of the solutions I have tried have worked, so I would appreciate it if someone could take a look at my code to see if I have some fundamental misunderstanding or application-specific problem.

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

EDIT: I forgot to mention this earlier, but I had tried some example PHP/AJAX code that others have posted online, and running that code did not work either, which makes me think that maybe this is a problem with my server configuration.

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

My Code: (Only includes files I believe to be relevant to the problem; if you would like to see the other files in the project, plase let me know.) The JavaScript and PHP files are in the same directory. I am using XAMPP.

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

joystick.js (Contains ajax call)

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 (File to which Javascript data should be sent)

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

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

Error Message:

错误信息:

Notice: Undefined index: x in C:\xampp\htdocs\TeachMoverInterface\ajax.php on line 2

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

The PHP $_POST array appears to be empty.

PHP $ _POST数组似乎是空的。

Possibly Helpful Browser Debugging Info:

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

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

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

As I said, I have been looking at forums for the past week, so some of the actions I tried in order to fix the problem have been to change formatting, change cache and async attributes in the ajax method, substitute the ajax call with a $.post call or XMLHttpRequest, replacing the JavaScript variable with a number in the ajax call, and some other methods I have forgotten.

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

3 个解决方案

#1


0  

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

modify your ajax.php code

修改你的ajax.php代码

#2


0  

As several people have pointed out, my screenshot of the Google Chrome console shows that the ajax.php file is receiving the data sent by the ajax call. My mistake was that I believed accessing the ajax.php file directly should output the data, however as Jay Blanchard pointed out that was an incorrect approach. Thank you for everyone who pointed out my mistake. Also, thanks to Jeremy Harris for mentioning an alternative to AJAX that may be better suited for my application in terms of time performance.

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

#3


-1  

Change data: {x:x_value}, into data: {"x":x_value},

将数据:{x:x_value}更改为数据:{“x”:x_value},

#1


0  

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

modify your ajax.php code

修改你的ajax.php代码

#2


0  

As several people have pointed out, my screenshot of the Google Chrome console shows that the ajax.php file is receiving the data sent by the ajax call. My mistake was that I believed accessing the ajax.php file directly should output the data, however as Jay Blanchard pointed out that was an incorrect approach. Thank you for everyone who pointed out my mistake. Also, thanks to Jeremy Harris for mentioning an alternative to AJAX that may be better suited for my application in terms of time performance.

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

#3


-1  

Change data: {x:x_value}, into data: {"x":x_value},

将数据:{x:x_value}更改为数据:{“x”:x_value},