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

125
June 29, 2018, at 06:20 AM

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.

HTML FILE
<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. 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. I want to echo that variable through php. But when I load the page, I get an error log stating that the code $btid = $_POST['btid']; has an Undefined Index.

Answer 1

I don't think your code is going to work as designed. You are using include("serverSide.php"); in the body of the HTML, but it is never going to have any $_POSTvalues unless you are posting a form.

Your ajax call is not doing anything with the value that is being returned.

I think you should remove the 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).

Answer 2

When you use PHP's include as in <?php include("serverSide.php"); ?> PHP will execute the code on the file being included. 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.

Your javascript will run on page load and make the ajax call correctly, but you are not using the response anywhere. In order to store the response from the Ajax call you need to add a success handler.

If I understood what you are trying correctly, your code should look more like this:

HTML FILE

<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. 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()).

Rent Charter Buses Company
READ ALSO
How do I create the str_shuffle from php in python

How do I create the str_shuffle from php in python

Note I don't want to use pythons shuffle as that uses the Fisher-Yates Shuffle algorithm I want to get the same result on both php and python when the seed (randomseed/srand) is the same

138
Custom Tab Form Submission Using Ultimate Member in Wordpress

Custom Tab Form Submission Using Ultimate Member in Wordpress

We have a wordpress site with active campaign and ultimate member installedOn our user account page there are several tabs created through ultimate members wordpress hooks

178
Convert Object to Array to display errors using Square API

Convert Object to Array to display errors using Square API

I'm currently implementing SquareUp Payment Method using their Connect V2 APIProcessing the payment works fine however I would like to echo any errors to screen lets say for example if the card declines

174
Laravel 5.6 Multi-relation eloquent query

Laravel 5.6 Multi-relation eloquent query

So I have the following database structure:

149