Reputation: 4043
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 = ????
?
Upvotes: 0
Views: 14069
Reputation: 101
The answer is to simply use Javascript to store the response from Ajax into a hidden variable and make use of that for further action. That solved my query and I'm sure many will actually need this too!
Upvotes: 0
Reputation: 4043
Here is the answer:
Get names in main.php using below way:
-------- filter.php ----------
session_start(); //at the top
$_SESSION['names'] = json_encode($rows);
-------- main.php ----------
session_start();
$issued_to = explode(",", $names);
session_unset();
session_destroy();
Upvotes: 0
Reputation: 74748
You can't use it that way.
Why:
javascript/jQuery is a scripting language which runs in browser when DOM is fully loaded and elements are available to make selectors.
While php is serverside language which runs on server way before page load.
So in short you can't assign a ajax response to a php variable.
one thing you can do to have a hidden input and put the response value in it and you can get that value to use it.
success:function(data){
alert(data);
$('#hiddenInputId').val(data); // set the response data to the hidden input and use it.
}
Upvotes: 2
Reputation: 634
You can create a div where you want to display the response contents
in your case it is after <script>
tag
And use innerHTML to display the contents
Use this code
<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){
$("#responseContent").html(data);
}
});
});
<script>
<div id="responseContent"></div>
Let me know if it is helpful
Upvotes: 1