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
Related
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/
Please bear up with me, I'm new to PHP. I'm using a mixture of PHP and jQuery on the same page. I need to pass variables from jQuery to PHP. All examples I came across describe how to pass variables to a PHP file on the server which I don't want. So far I manged to convert object to JSON using jQuery $.toJSON();
My question is: is it possible to pass jQuery data to php code if both jQuery and PHP reside on the same page?
Here is my code so far:
var myObject = {name: 'Tomas', age: 38};
var encoded = $.toJSON( myObject);
var name = $.evalJSON( encoded ).name;
var version = $.evalJSON(encoded).age;
No it is not possible since PHP is not a client side script but rather a server side script. What that means is that by the time the data is given to the client the PHP script would have most likely already finished running. The following demonstrates the relationship between PHP and JavaScript:
Server -> PHP -> Client (Browser) -> Javascript
Therefore, it is impossible to have javascript communicate to PHP on the same page. What you can do is use Ajax to call a server side file like you said - but I'm afraid that is as much as you can do in terms of PHP and JavaScript Communication
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Calling PHP Function within Javascript
I have a small JavaScript code in a php file. This JavaScript code runs some logic and in the end should call a php function which is defined in the same php file. How can I do this ? any guidance ?
you can use ajax for that and call this same file. because php function will not initialize from javascript.
I think using PHPLIVEX, can be useful too in this scenario.
PHPLIVEX has a little tag line -
Call your php functions and class methods from javascript in one line
This can be useful if you need to call php functions from javascript several times in your application.
if an server-side code need to be executed , you need to call that via ajax.
this is for sure not the best answer there is, but it should work: have your javascript make an ajax call, to your same page, using a POST parameter, so u know this is the call for that function .. hope it is clear enough in large lines ..
You cannot. JavaScript is executed in a browser, PHP on server. The only way to do it is using AJAX request, e.g. with jQuery:
$.get('myurl.php', function(data){ ... });
http://api.jquery.com/jQuery.get/
That will return output produced by myphp.php file; if you want to run a single function from it, you'll need some logic that determines which function it should execute, e.g. URL parameter. However, I'd strongly advise against using url parameter value as direct function name.
you should use jquery's ajax or post
I have a Javascript variable which I am setting a PHP variable to.
function cancel(number) {
var message = "<?= $message[" + number + "]; ?>";
}
$message is an array. "number" is the element of the array I want to set message to. Basically, I want to set a Javascript variable to a PHP variable using a Javascript variable as the element picker. So if "number" was 2, it would select:
$message[2];
However, the above approach doesn't work, and I'm not even sure if this is possible.
It isn't. Use XHR to retrieve the value from the server.
It doesn't seem at all possible; PHP is evaluated server-side, and javascript is evaluated client-side. So PHP would see it as $message["+number+"], and try to find the value at the index of "+number+". You'd probably have to do something like an AJAX request to get the data you're looking for.
What you are doing isn't possible; since php is a server-side language, it's executed first, and the js is executed after; there isn't any way to control which is executed first. You must retrieve the variable using AJAX.
Something like this will work:
<script type="text/javascript">
var messages = <?= json_encode($message) ?>;
function cancel(number) {
var message = messages[number];
}
</script>
Of course this will output the entire array in the JavaScript source. If it is large, then you are better off using AJAX.
Tip: if you "view source" it should be painfully obvious why your method doesn't work.
You simply cannot do this using this methodology. PHP is server-side code, meaning that it runs on the server, while JavaScript is client-side code, meaning it runs on the client, or your browser.
Once the PHP runs, it generates an HTML document and sends that document in the response to the browser. Once that's complete, the only way you can get data back to the server is to send it via a form POST, send it via AJAX, or send it via script tag remoting.
Consider looking at some examples on the Internet of how to POST data back to the server via a form and via AJAX. It's clear you're struggling with some concepts regarding how to properly architect your program, and looking at some examples would be a great way for you to learn and master these techniques.
PHP Submit Form Example
PHP Tutorial
You need to use AJAX call to resolve your issue.
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.