This question already has answers here:
How do I pass JavaScript variables to PHP?
(16 answers)
What is the difference between client-side and server-side programming?
(3 answers)
Closed 2 years ago.
I am not familiar with Javascript at all. I need to retrieve the value from externalReferralProgram . Which is in json format.
I want to get the value of this and store it in a PHP variable.
const FTXRest = require('./');
const ftx = new FTXRest({
key: 'apikey',
secret: 'apiseceret'
})
ftx.request({
method: 'GET',
path: '/api_key_status'
}).then(console.log);
PHP is a server-side language. Javascript is a client side language.
What happens is PHP will run whatever stuff its supposed to, then send whatever HTML and javascript you tell it to send to the browser. At this point, PHP will wipe out this session and start handling other requests. The browser then receives this content and starts running the javascript.
With this understanding, you can see that it really doesn't make sense to store a javascript value in a PHP variable - by the time the javascript is running, PHP has already long-forgotten about this request.
Your best bet is to either find a way to do the same javascript logic in PHP, or, make the javascript send a REST request back to the PHP server with whatever data you need, so that PHP can do further processing on it (this means creating a separate PHP file that'll receive javascript data from $_GET or $_POST).
Use json_decode
Takes a JSON encoded string and converts it into a PHP variable.
Related
This question already has answers here:
How do I pass variables and data from PHP to JavaScript?
(19 answers)
Closed 3 years ago.
So my website has received some data in the form of a PHP associative array (see below) and I'm trying to convert it to something I can interact with using JavaScript.
JavaScript seems to treat it as just a string and I've had problems using JSON.parse. I have control over the format of this data before it arrives at my JS do I need to restructure it??
Extra Info: Web page A requests data from web page B then web page B collects the data from a database and returns a big array. The array above is the response from web page B in web page A. Each index (["tag"],["current_ids"] etc) actually have large array contained in them, I just shortened it to 1 for ease of understanding.
Data on the web is always sent and received as strings. JavaScript needs the data in a format that it can understand, for example JSON.
PHP has an in-built function to convert your data, e.g. your array, into JSON called json_encode(). It also has a sister function for decoding JSON into PHP called json_decode().
https://www.php.net/manual/en/function.json-encode.php
https://www.php.net/manual/en/function.json-decode.php
Depending on your setup, you'll want JavaScript to make a XHR request to an endpoint where you're running PHP and then you'll use PHP to process that request and return a JSON string.
Alternatively, you could use PHP to directly echo the JSON string into JavaScript, for example:
<script>
<?php echo "var myData = ${json};"; ?>
</script>
XHR is the better practice in general but you should use whichever approach best suits your needs.
This question already has answers here:
How to use javascript variables in PHP [duplicate]
(4 answers)
Closed 4 years ago.
I made a javascript variable and assign some value
<script>
var score = 25;
</script>
after that i need that value inside php for any reason, unable to understand how to do so.
As JavaScript is a client-side language and PHP is a server-side language you would need to physically push the variable to the PHP script, by either including the variable on the page load of the PHP script (script.php?var=test), which really has nothing to do with JavaScript, or by passing the variable to the PHP via an AJAX/AHAH call each time the variable is changed.
If you did want to go down the second path, you'd be looking at XMLHttpRequest, or my preference, jQuerys Ajax calls: http://docs.jquery.com/Ajax
This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 5 years ago.
I am working in PHP, and I call a Javascript function like this:
?><script type="text/javascript">send_information();</script><?php
After that, I change the value of a session variable and refresh the PHP page (after refreshing some stuff is displayed on my web app)
I would like to wait until the execution of my javascript function is done before refreshing the page.
I looked for something like a timer but did not find what I was looking for, also I looked for a way to send a variable from JS to PHP at the end of execution and put a "while" in my PHP, waiting for this variable but I understood that this is not possible...
(I am not looking for the difference between client side and server side, this question is totally different! :) )
You can't do what you wish to do.
The php part of the code is generated on the webserver. This generates a bunch of text which send as a whole to the client. PHP dies.
The browser on the users computer then interprets this text and displays it. This computer doesn't know anything about what the servers php code wishes to do. It only sees a ready text.
It then executes the javascript code.
Essentially, the question I am asking is similar to this question. However, these Q/A's were of little value to me.
Firstly, there are several people saying that it is impossible to pass a value from a clientside language to a serverside language. However, I know that this is untrue, since I know that it is possible to pass javascritpt (clientside) variables to php (serverside) variables.
Secondly, my need to do this is based on trying to retrieve data from a 3rd party website, to be processed by my program, which will not attempt to return this data to the origional web page. Instead it will be returned to a different location. So, suppose I am retrieving the data required from their webpage using javascript and storing it within a Javascript variable, say var myData = *my required data* and I wish to create a Java program which accepts this data as an input, how would this be done?
You can always send client side information to the server. This works in Java as well.
You can make an AJAX request and pass your variable with it.
$.ajax({
type: 'post',
url: 'myserverurl.ajx',
data: 'data=' + variable1 + '&data2=' + variable2
});
You can do this without having to make an async call as well.
This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 7 years ago.
I have this function in my PHP code.
<?php
$user = get_user_by_id($user_id);
?>
And I also have a JavaScript code on the same page.
The JavaScript generates some value to a JS variable.
What I need to do is, using that JavaScript variable value as the argument of the above PHP function.
So, what's the simplest method for achieving this..?
That's not quiet possible. You can't use client side variables (ECMA Script) inside your server side scripts (PHP). You may be able to achieve this by creating a web service and passing the variables using XHTTP Requests (AJAX) or reload the page using ECMA Script passing the value as a parameter and using it server side.
I don't know what exactly you are trying to do so I can't give you a definitive answer to this.