jquery - How to pass variable in Ajax, PHP
Get the solution ↓↓↓Solution:
Your AJAX calls to the page each initialise a new set of variables. You can't access $arrray from consecutive calls, but you could access a session variable.
Addsession_start();
at the top of the page, then
<?php
//first Ajax call
if(isset($_POST['id'])){
$_SESSION['id'] = $_POST['id'];
$_SESSION['value'] = $_POST['value']; //The first call is mainly to update the value of $array
echo $_SESSION['value'];
exit();
}
//Second Ajax call
if(isset($_POST['name'])){
echo $_SESSION['id'],"<br>";
echo $_SESSION['value'],"<br>";
}
Note: this snippet is making assumptions about the order of execution that might not be justified. For production code you should test the presence of$_POST
and$_SESSION
variables before you use them.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: foreach() argument must be of type array|object, null given
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.