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
.
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 $_POST
values 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).
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()
).
Firebase Cloud Functions: PubSub, "res.on is not a function"
TypeError: Cannot read properties of undefined (reading 'createMessageComponentCollector')
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
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
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