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.
Related
This question already has answers here:
How do I pass variables and data from PHP to JavaScript?
(19 answers)
Closed 8 years ago.
I need a php variable inside js code.
Why this doens't work?
index.php
<?php
$h = 966;
?>
index.js
var b = <?php echo $h;?>;
Error:
Uncaught SyntaxError: Unexpected token <
You can't run php inside a JavaScript (.js) file.
Take a look at the question shared by Ed Cottrell in the comment on your post for more information, but basically, PHP is a server language, whereas JavaScript is a client one—when your computer loads index.js, it sees the php script, but has no idea what to do with it.
A straightforward solution (though not the best) would be to output a <script> tag containing your variable inside from your php file— and then access it from your JavaScript file.
When your browser requests the Js file, the web server typically simply sends it the file in the response without processing it like a php file. It doesn't know you have php code in it. It does the same (just sends it to the requesting browser) for images, text files, etc.
You can try doing that in your php view/template/script (I would wrap the output in single quotes). Something like:
<script>var b = '<?php echo $h;?>';</script>
You could also make a JavaScript file with a php extension: index.js.php, and include this instead of index.js. You'll need to set some headers in the top of that file to let the browser know it's getting JavaScript:
header("content-type: application/javascript");
There is some slightly more advanced stuff you can do as well: use an htaccess file to parse certain requests (like for that Js file) using php, or using an asynchronous request (Ajax) to pull the value of 'b' from the server, but it looks like your doing something a bit more simple.
This question already has answers here:
how to write php function inside javascript function
(11 answers)
Closed 9 years ago.
There is a javascript function with a parameter :
<script type="text/javascript">
function afficheHelp(aide_htm)
{
... // I want to code PHP here with the parameter "aide_htm"
}
</script>
How to use the "aide_htm" javascript parameter inside PHP code within the javascript function ?
You need to invoke serverside code from your clientside Javascript. This is commonly done with an Ajax call. How to exactly do this depends enormously on what you want to do with the result and which JS library you are using. I'd recommend reading up on Ajax in general, and jQuery or Mootools on how to execute a call specifically.
Javascript is client side and executed in the users browser whereas PHP is server side and executed on your server as such you can't use javascript directly in PHP. You can however use javascript to perform an AJAX request to PHP on your server. If you're using jQuery have a look at http://api.jquery.com/jQuery.ajax/
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 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.
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.