0

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 = ?????

4 Answers 4

2

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.
}
Sign up to request clarification or add additional context in comments.

Comments

1

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

7 Comments

You didn't read the question. I want to take in PHP variable, not in JS div or HTML.
Then you can't get the Ajax response in PHP variable
Technically you can't pass any current page javascript variable to current page php. That's why we are going for ajax. Ajax will pass javascript variable to another php file
For that reason, i have stored value in another PHP file. Is there any way to get value from there? I haven't used isset something like this but any way?
you can set a session variable there from the source php file and you can read the session variable in another php file wherever you want
|
0

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

Comments

0

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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.