This question already has answers here:
How to access PHP variables in JavaScript or jQuery rather than <?php echo $variable ?> [duplicate]
(6 answers)
Access PHP variable in JavaScript [duplicate]
(3 answers)
Closed 10 years ago.
How to use javascript to access php variable?
Does only one way to write code (like var a={?echo $variable})in javascript?
Recommend some books on php and javascipt(include projects)?I don't know how to use knowledge in projects?
Yes, you were correct.
Because PHP is executed before JavaScript, you can't change it later on, but you could do something like this:
<? $aVar = "whatever"; ?>
...
<script>
var aVar = "<? echo $aVar; ?>"; // note the quotes! (SO's highlighter renders this incorrectly, starting a PHP block inside quotes is valid and will be recognized.)
</script>
That will send this to the client:
<script>
var aVar = "whatever"; // note the quotes!
</script>
var a=<?php echo $variable; ?>
PHP runs in server and js in client side, so Iguess there is not other you can get it. OR you need to use AJAX
I don't see how can you do that in any other method. PHP is server side, while JS is executed on client's PC (not on the web server). Theoretically and practically it's not possible.
Related
This question already has answers here:
How can I pass variables from JavaScript to PHP?
(2 answers)
Closed 9 years ago.
I want to pass a variable from a function in JavaScript to PHP that is also inside the JavaScript function.
For Example:
myfunction('qwerty');
<script>
myfunctionjs(x)
{
<?php echo $x?>
}
</script>
note: code above is just a sample
Your code sample, while it expresses what you want quite clearly, should also show how impossible it is.
PHP is a server-side language whereas javascript is a client-side language. The only way for javascript to "talk" to PHP is to submit a request to the server. You can do this either through a standard HTTP-GET request or a POST request (through a form submission, or more commonly via Ajax).
The PHP isn't inside the JavaScript function. The PHP code is executed on the server.
When the Javascript is executed, the code looks like this:
myfunction('qwerty');
<script>
myfunctionjs(x)
{
alert(x); //or whatever $x is
}
</script>
If you want to pass 'x' to a PHP script, you'll have to make an AJAX request.
You can't have javascript telling PHP to do something inline. JavaScript runs client side, while PHP runs server side.
Instead, you need to have your javascript send the data to your server, and you'll have PHP do something when receiving that second request.
If you are using jQuery, have a look at the ajax method for example on how to send data: http://api.jquery.com/jQuery.ajax/
Passing variable from javascript to php right away is not possible. You will have to use AJAX for this. You have to understand the difference between server-side scripting and client-side scripting.
https://softwareengineering.stackexchange.com/questions/171203/what-are-the-difference-between-server-side-and-client-side-programming
Read the above link and it will give you a full understanding of the process. if you have any issues let me know.
Read more
This question already has answers here:
How do I pass variables and data from PHP to JavaScript?
(19 answers)
Closed 8 years ago.
Possible Duplicate:
How to pass a variable / data from javascript to php and vice versa?
I have a file of php and javascript code. I want to set a php variable to be the result of a javascript function which takes a php variable as a parameter. For example:
$parameter = "this is a php variable";
$phpVar = echo "foo(" . parameter . ");";
I know you cannot do a "= echo", is there another way I can do this?
Thanks
You can't directly do that, since JavaScript runs on the client-side and PHP gets executed on the server-side.
You would need to execute the JavaScript first and then send the result to the server via a FORM or AJAX call.
Here's what that might look like:
PHP
$parameter = "this is a php variable";
echo "var myval = foo(" . parameter . ");";
JavaScript
var myval = foo("this is a php variable"); // generated by PHP
$.ajax({
type: 'POST',
url: 'yourphpfile.php',
data: {'variable': myval},
});
Receiving PHP (yourphpfile.php)
$myval = $_POST['variable'];
// do something
PHP code is run on the server before the response is sent; JavaScript is run after, in the browser. You can't set a PHP variable from JavaScript because there are no PHP variables in the browser, where JavaScript if running. You'll have to use an AJAX request from JavaScript to a PHP script that stores whatever information it is you want to get.
Try this:
var a = "Result: "+ <?php echo json_encode($parameter); ?>;
You have to make a GET or POST request to the script from JavaScript.
You cannot pass a js variable to php code.
PHP happens to run on the server, a thousand miles away from client, where js is run.
So, you can only call a php script, using js or regular hyperlink.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to get javascript variable value in php
I am looking for help to set value of a php variable from javascript variable.
I tried these but nothing worked:
Try 1:
<script>
<?php $phpVar = ?> jVar;
</script>
Try2:
<?php $phpVar = echo "<script>document.write(jVar)</script>";
Failed too..
Please help.
Please help!
Actually php is server side scripting language (that runs at server) and javascript is basically client side programming language (which runs in the browser) so you can't set a php variable from javascript. Look at these tutorials php and javascript.
But using ajax (javascript) you can pass javascript variables to php and can use them in the to the php script. Here is ajax tutorial link.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to pass value from javascript to php file
would something like this work?
<script>
var a;
a = 5;
<?php
$b = ?>a;
</script>
I am wondering if I can have javascript parsed by the client-side and have the output information be picked up by PHP as a variable that can be used elsewhere.
You could actually have your JS parsed with PHP if you:
configured your web server to parse .js files, or,
gave your JS file a .php extension and configured the response header with the appropriate content type
e.g.:
<?php header('Content-type: text/javascript');?>
You'd also need to have a PHP include or at least the relevant code to populate the required variables.
Note it's best practice to not use PHP short-tags - your php.ini should be configured to not allow them. i.e., do this:
<?php echo %b %>
instead of this:
<? echo %b %>
that way you cant. The file is first parsed by PHP on the server and then sent to the client-side, where the javascript is parsed.
Without ajax or other kind of requests, data is not sent back to the server.
no, you can't
<script>
var a;
a = 5;
<?php
$b = ?>a;
</script>
on the contrary, you can do this.
<script>
var a;
a = <?echo $b?>; // $b will be the variable of a in javascript (client side)
</script>
This question already has answers here:
How do I pass variables and data from PHP to JavaScript?
(19 answers)
Closed 8 years ago.
I am trying to create a site where someone can create an "item" and the database will store an id and php generates a url for that id. So next time when the person comes back with that url it will remember the person's settings (variables). Now the problem is that in my site javascript need to know these variables.
So what is the best solution for this? passing the variables in the superglobal "GET" or maybe cookies? Or is there a better way to pass these variables to javascript?
just use php to print some dynamic javascript
<script>
var myVar = "<?php echo json_encode($_COOKIE['somevalue']);?>";
</script>
There are multiple methods for providing the data to the client, such as:
Echo the variables in your javascript, var userid = <?php echo
$userid; ?>
JSON'fy your variables and supply them to your javascript via AJAX/jQuery: $.getJSON(url, function(data){ var userid = data.userid; });
I typically utilize JSON as much as possible when trying to present server-side data to the client-side, as it helps to separate the different layers.