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.
Related
This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
How do I pass variables and data from PHP to JavaScript?
(19 answers)
Closed 7 years ago.
Hi again are there a way that i can get the variable "log" in a jquery code like the example bellow with out using alert, a field id, class and etc.
<script>
var log='1';
</script>
<?php
$run = log
?>
I want to get the data of a certain variable in a javascript and use it as a value of a variable in my php code
You can't do it that way.
Because:
javascript runs in browser.
PHP runs at server.
There are two ways one is form submission and other one is ajax. You have to send the values to the server to get it to echo.
As per your updates, i would suggest you to use ajax/form submit:
Ajax:
var log = '1';
$.ajax({
url: 'path/to/php/file.php',
type:'get',
data:{log:log},
success:function(data){
console.log(data); // logs: The posted value is 1
}
});
here in the data object log before : is the key while log after : is the var which refers to value '1'. So, in php you can do this:
<?php
if(isset($_POST['log'])){
$run = $_POST['log'];
echo 'The posted value is '.$run;
}
?>
You cannot get client side variable on server side without the use of AJAX or some sort of post back. In all calls to a server, the server side code is execute then returned to render client side in the browser. Therefor, the client side code is ALWAYS run after the server side.
You can look at using client side code to make an AJAX request to the server and return a value or post to to a page which will load and render it server side.
You are trying to echo a value here, this can be done client side but if there is complex server side logic to get the value then I would use an AJAX request to get it.
AJAX
http://www.w3schools.com/jquery/jquery_ajax_intro.asp
POST a form
http://php.net/manual/en/tutorial.forms.php
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:
Closed 10 years ago.
Possible Duplicate:
How to call a PHP function in JavaScript?
i have a form to print to a POS printer. Problem 1 is that i cant print directly, Mybe in google chrome in kioskmode.. so. i would like to call a function after the windows be closed from printing. code is like this:
<script type="text/javascript">
$(function(){
$('#printOut').click(function(e){
e.preventDefault();
var w = window.open();
var printOne = $('.contentToPrint').html();
var printTwo = $('.termsToPrint').html();
w.document.write('<html>' + printOne + '<hr />' + printTwo ) + '</body></html>';
w.window.print();
w.document.close();
I INSERTED HERE>>> insert(); <<<<<<<<<<< AND WORKS BUT NOT BY CLOSING THE priter WINDOWS. I dont want to close the BROWSER!
return false;
});
});
</script>
where can i call the php function insert();
You can't call a PHP function since it's on the sever and your JavaScript code is on the client. You can navigate to a PHP containing that function or make an AJAX call to that page.
Navigating via Javascript would be:
document.location = "yourpage.php"
You cannot call PHP functions in the classic sense with Javascript. PHP code is run on the server, while Javascript code is executed in the user's browser window. What you can do however is use a technology called AJAX to send a request back to your server. Using HTTP methods you could initiate a PHP script that returns information back to your Javascript code also over HTTP.
I have some PHP code inside the script so I need
the JavaScript value as a PHP variable, as follows:
<script>
;
var js_var = 123;
var php_var = <?php js_var ?>;
alert(php_var);
;
</script>
Using jQuery to send the value to a PHP page will return the value but not as a PHP variable:
$.get("page.php", {js_var_name: js_var}, function(data) {alert(data)});
I tried also all jQuery AJAX functions: load(), post() and the ajax() method, none of them pass back the value of PHP as above requested.
Is it possible to implement the above with jQuery?
Likely you have a syntax issue with :
var php_var = <?php js_var ?>;
Unless you used define() your php variable should start with $ and you need to echo it so that it gets printed to the page
var php_var = <?php echo $js_var ?>;
If you are assuming that php will read the prior javascript js_var=123 it won't. Javscript is a client side language and php is server side.
You cannot simply mix PHP and JavaScript. The former is executed on the server, the latter on the client, namely in the user's browser. In order to hand a value from PHP to JavaScript you need to output JavaScript code via PHP or do a AJAX-Request to a PHP-Script, as you tried. When using AJAX, it is the easiest solutions to have PHP script that returns a JSON string. When calling this script using $.get() the result will be stored in a JavaScript variable and you can access all the values from there.
Take a moment and a deep breath...and then start thinking about your scopes again.