简体   繁体   English

使用Ajax Echo的未定义索引将Javascript变量传递给PHP

[英]Passing Javascript Variable To PHP Using Ajax Echo's Undefined Index

I minimized the code snippets to show only the code needed, and the url for the server side file is actually connected to a url on my server. 我最小化了代码片段以仅显示所需的代码,并且服务器端文件的url实际上已连接到服务器上的url。


HTML FILE HTML文件

<head>
    <script>
        var btid = 1;
        $.ajax({
            url: "serverSide.php",
            method: "POST",
            data: { "btid": btid }
        });
    </script>
</head>
<body>
    <?php include("serverSide.php"); ?>
</body>

serverSide FILE 服务器端文件

<?php
    $btid = $_POST['btid'];
    echo($btid);
?>

DESCRIPTION 描述

So what is going on is when the page loads, the javascript code runs. 因此,发生的情况是在页面加载时,javascript代码运行。 It creates a variable named btid equal to 1. This variable is then sent to a file on my server that is a php file. 它创建一个名为btid等于1的变量。然后将该变量发送到服务器上的php文件中。 I want to echo that variable through php. 我想通过php回显该变量。 But when I load the page, I get an error log stating that the code $btid = $_POST['btid']; 但是,当我加载页面时,我收到一条错误日志,指出代码$btid = $_POST['btid']; has an Undefined Index . 有一个Undefined Index

I don't think your code is going to work as designed. 我认为您的代码无法按设计工作。 You are using include("serverSide.php"); 您正在使用include("serverSide.php"); in the body of the HTML, but it is never going to have any $_POST values unless you are posting a form . 在HTML正文中,但除非发布form否则它永远不会有任何$_POST值。

Your ajax call is not doing anything with the value that is being returned. 您的ajax调用对返回的值不执行任何操作。

I think you should remove the include("serverSide.php"); 我认为您应该删除include("serverSide.php"); from the body of your HTML (it is serving no purpose in its current incarnation) and use the returned value of your ajax call to put the value of btid in the HTML (if that is where you want it). 从您的HTML正文(在当前版本中没有用)开始,并使用ajax调用的返回值将btid的值btid HTML中(如果您想要的话)。

When you use PHP's include as in <?php include("serverSide.php"); ?> 当您使用<?php include("serverSide.php"); ?> <?php include("serverSide.php"); ?> PHP will execute the code on the file being included. <?php include("serverSide.php"); ?> PHP将在包含的文件上执行代码。 That is what is causing your error, when the code is first evaluated it has no $_POST['btid'] because you haven't called it yet. 这就是导致您出错的原因,在首次评估代码时,它没有$_POST['btid']因为您尚未调用它。

Your javascript will run on page load and make the ajax call correctly, but you are not using the response anywhere. 您的JavaScript将在页面加载时运行,并正确进行ajax调用,但是您没有在任何地方使用响应。 In order to store the response from the Ajax call you need to add a success handler. 为了存储来自Ajax调用的响应,您需要添加一个成功处理程序。

If I understood what you are trying correctly, your code should look more like this: 如果我理解您的尝试正确无误,则您的代码应该看起来像这样:

HTML FILE HTML文件

<head>

</head>
<body>
    <div id="response"></div>

    <script>
        var btid = 1;
        $.ajax({
            url: "serverSide.php",
            method: "POST",
            data: { "btid": btid },
            success: function(res) {
                $('#response').text(res);
            }
        });
    </script>
</body>

What we are doing is making the ajax call and when the call is successful we assign the returned value as the div content. 我们正在做的是ajax调用,当调用成功时,我们将返回的值分配为div内容。 Also, I switched the script tag to the end of the body because we need to be sure all the document has loaded before changing anything (could have used $( document ).ready() ). 另外,我将script标记切换到主体的末尾,因为我们需要确保在更改任何内容之前可以加载所有文档(可以使用$( document ).ready() )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM