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.
Related
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.
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.
This question already has answers here:
Access Java / Servlet / JSP / JSTL / EL variables in JavaScript
(5 answers)
Closed 6 years ago.
First of all, I am not sure if this is even possible due to one being server side and other being client side.
Application that I am working on uses Spring MVC 3.1 and there is a Map (lets say validValuesMap ) of type Map<String,Long> defined in Spring method - referenceData.
Due to Map being defined in referenceData method, this Map is available in JSP page and I can use JSTL on this Map like - <core:if test="${(validValuesMap ne null)}"> etc.
Can I use this Map in plain JavaScript or jQuery too?
I have to basically perform a simple check on Long value of this Map if that is being greater than zero for a key and that key is available in the JavaScript function as a var.
I have to put that check into an else if (....) of JavaScript.
Is that possible? or any other way to perform that validation on client side for a button click event?
Convert that object to JSON on server side itself and then store it in javascript variable to manipulate it later on client side .
This question already has answers here:
Access a JavaScript variable from PHP
(9 answers)
What is the difference between client-side and server-side programming?
(3 answers)
Closed 8 years ago.
can I assign value to php variable with java script. I want to copy a java script variable's value to php variable . Is it possible ?
Javascript is on the client side, whereas PHP executes on the server side. PHP variables can be assigned to javascript variables, but otherwise no.