简体   繁体   中英

changing php variables with javascript after API call

I am struggling with something lately. I have a bit of javascript code calling an API and than gathering database data according to the API response here is how I do it :

<?php
    $class = MyClass();
?>

<div id="display"></div>
<span id="foo"></span>

<script type="text/javascript">
var p = document.getElementById("foo");

p.onclick = function() {
    // API Call
    $.post("CallAPI.php", {
        parameter1: someValue,
        parameter2: someOtherValue,
    }).done(function (data) {
        json = JSON.parse(data);
        response = json.node.subNode.field;
        // Database Fetch
        $.post("fetchDataFromDB.php", {
            parameter: response,
        }).done(function (data) {
            // what do I do ?
        });
    });
};
</script>

On top of that I have an instanciated PHP Class I would like to modify according to the data fetched on the database and then output some variable from that class on the front.

For simplicity's sake let's say the class is something like this :

class myClass
{
    private $_variable;

    public function __construct(){
        $this->_variable = 'init';
    }

    public getVariable(){
        return $this->_variable;
    }

    public setVariable($variable){
        $this->_variable = $variable;
    }
}

What I am looking for is a way to modify $_variable by affecting it the value return by the database fetch and then displaying the value of $_variable in the display <div> .

This is not possible, because of the way PHP works. A PHP script gets loaded into the interpreter, get excecuted and then unloaded. The following explanation isn't 100% accurate, but it might help you understand better:

  • You use JS to do the API call to CallAPI.php
    • CallAPI.php does it's magic, returns possible output, the script stops .
  • The JS catches the output, and calls fetchDataFromDB.php
    • At this point, every value that existed in CallAPI.php , doesn't anymore
    • fetchDataFromDB.php does it's magic, returns possible output, the script stops .

It's a confusing concept to graps when you start coding, but PHP is syncronice (first A, then B, then C, then done) and javascript is async (there is not perse a given order to ABC, some might fire right away, some wait for something to happen frist, like a click).

It's a bit difficult to understand what you want, as your variable names are not good ( parameter1 is not descriptive at all ), so I can't give you a more exact answer.
In the case you specify in you question, if you don't use the values in javascript, simple maybe the first php script call the 2nd php script directly. A lot faster too.

If you have a situation as you describe, where the result is needed in javascript for *reasons*, you can store the value in session variables, or in the database.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM