I have three php files.
main.php - to use stored Ajax response.
filter.php - to send Ajax request and get response
insert.php - to store Ajax response for using in main.php
Primary purpose of doing all these thing is to use client side values using PHP code because server and client can't exchange variable values each other.
The response should be stored in php variable in main.php
.
main.php:
?>
<script>
$.ajax({
type: "POST",
url: "filter.php",
data: { id1: name, id2:"employees"},
success:function(response) {
var res = response;
$.ajax({
type: "POST",
url: "insert.php",
data: { id1: res },
success:function(data){
alert(data);
}
});
});
<script>
<?php
$ajaxResponse = ???? <need to get value of data over here>
filter.php:
// Return employee names
if ($_POST['id1'] == "name" && $_POST['id2'] == "employees") {
$conn = mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT " .$_POST['id1']. " FROM 1_employees";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_array($result)) {
$rows[] = $row['name'];
}
}
echo json_encode($rows);
mysqli_close($conn);
exit (0);
}
insert.php:
if ($_POST) {
if ($_POST['id1'] !== "") {
echo $_POST['id1'];
}
}
So how can I get ajax response value in main.php at $ajaxResponse = ????
?