Is it possible to use JavaScript's variable in PHP? [duplicate] - php

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Access a JavaScript variable from PHP
theVariable = String.fromCharCode(theVariable).toLowerCase();
is it possible to use theVariable in PHP codes ? How?

Not really... you can send it to a PHP script using AJAX or an HTML form. I strongly recommend you get a clearer understanding of what JavaScript is vs. PHP, particularly what it means that JavaScript runs on the client-side (i.e. in the browser), and PHP runs server-side.

Only if the the variable is sent via ajax to a PHP page. You see PHP is executed first, then the page is served to the client. Then the client executes the javascript. So the javascript is executed long after the PHP has been executed.

To use a variable that is in JavaScript in PHP, you have to send an Ajax request to a PHP page passing that variable in. This probably isn't the effect you're looking for.
JavaScript executes on the viewer's computer. PHP executes on the server. To send a JavaScript variable to PHP, you need to send some packet of information from the client to the server. This is done as a GET/POST request.

The only way to pass a variable from JavaScript to PHP is to pass it by an AJAX call.

Related

How to access the HTML table from PHP [duplicate]

This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 8 years ago.
I have a page with a java script that adds a row to a table. On the same page there is a PHP script that should read the table and add it to the database. How do I read the table using PHP ?
What you're thinking of will not directly work.
A Javascript script is executed on the client side only, whereas a PHP is executed on the server side only, and before the datas are sent to the client.
PHP can not directly read the table.
What you can do is use AJAX to send the data from the user's browser to a PHP script that'll use them.
You'll find more infos here.
PHP is serverside programing, the PHP code serves data to the client - PHP can't read from the client.
If you add a row using javascript, PHP has no way of getting that info.
You'll need to use Ajax to solve your specific problem.
You cannot "read the table" with PHP. You will have to send the data of the table to the server (and read it with PHP) using JavaScript or CGI.
A well-known technique to do this is called Ajax.

retrieve name of calling script / Javascript in php [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Securing a remote ajax method call
Is there a way to retrieve the name of the javascript file that called a php script from within the PHP code?
Unfortunately, since I have to call a PHP script from Javascript (for Google maps) on a webpage, this 'invites' to retrieve my data by just calling the PHP script by hand. Checking for the script/file that invoked the PHP script would bring at least some protection.
You could set a session variable with some random value on the server when your page is opened, and add that variable in the query string to your php script.
Then in your javascript-called php script, you start the session and check the session variable against the query one.

passing variable javascript into php [duplicate]

This question already has answers here:
How do I pass variables and data from PHP to JavaScript?
(19 answers)
Closed 8 years ago.
I want to pass a variable javascript into variable php. I can't use $_GET or $_POST because is all loaded by the method load in jquery and I don't have any form or possbility to implement it.
This is my code:
<script type="text/javascript">
var id= "test";
</script>
<?php $_SESSION['my_page'] =?> + id;
How can I resolve It?
2 solutions
you use AJAX to send your value, see jQuery.
you set that session variable from a submitted form as a value and process it on your script.
Eighter way you CANNOT mix javascript with PHP like that. Javascript is client side, PHP is server side.
for jQuery:
<script type="text/javascript">
var id = "test";
$.get('yourScript.php?id='+id);
</script>
and in yourScript.php
<?php
session_start();
$_SESSION['my_page'] = (int)$_GET['id'];
?>
The truth is you can't really pass them. Javascript is client-side meaning it happens on each individual computer. PHP is server-side so it happens on the server. You would have to somehow pass that variable from Javascript into the server and you could do that AJAX.
Another idea to consider is using the standard way of passing variables into different PHP documents: forms. You can make a form and use javascript to insert what you want to be submitted into the PHP server and from there you can use the $_GET or $_POST. Hope this helps.
Consider using AJAX. Here is a link to some tutorials although if you are using JQuery, it can handle most of the coding for you.
http://www.w3schools.com/ajax/default.asp
From JavaScript, you can call the PHP script after the page has loaded asynchronously (meaning that it will open a connection in the background rather than changing the page). Once the PHP script has finished executing, it will return a status code that you can check for through the JavaScript. You can access the data resulting from the PHP scripts execution in the JavaScript.
If you are coding a mobile website, be aware that not all mobile phones support AJAX.
EDIT: If you would rather use JQuery, then here is a link to a good tutorial http://www.sitepoint.com/ajax-jquery/

passing javascript value to php function [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to pass JS variable to php?
pass a js variable to a php variable
I'm asking if it is possible passing value obtained through javascript function in a php function in the same file.for instance: I have a javascript function that calculate my position and pass this value to php function.is it possible?
[Edit]
Well I think I should be more specific...What I want to do specifically is to calculate my position through geolocation library of google API and return the value of latitude and longitude to a php variable in the way I can use it inside my php page. BUT this page it is not a normal php page it is a codeigniter Controller since I'm using MVC model to handle my project
Yes it is, try to look into AJAX. With help from a library as jQuery and it's ajax methods should get you going.
To call back to a server-side script from the page, you can use an XMLHttpRequest (or a suitable wrapper such as jQuery).
simpler way would be to create hidden form field and set its value in js and get it through php.
Yes, there are two ways
send the data directly using an ajax request, which doesn't require the whole page to be submitted
use javascript to write the value to a hidden form element/cookie which will then get passed to your php script (either in $_POST or $_COOKIE) when you request the page again
No, php is a server-side language as opposed to javascript, which is a client-side language.
However, you can send an AJAX response with the position variable to a PHP script (try using the jQuery post function: http://api.jquery.com/jQuery.post/)

Assign JavaScript variable to Smarty variable [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
how to assign a javascript variable to a smarty variable
I want to assign a textfield value to Smarty variable using JavaScript and call Smarty function but I could not do it.
Please guide me.
You cannot do that in an easy way. PHP, and by extension Smarty, is parsed and run on the server side before the browser get the data. JavaScript is run on the client side when the browser parses the HTML, CSS and Javascript that the server sent.
You will have to make a new HTTP request in some way, sending the new data. You can do that by reloading the entire web page and sending stuff in the querystring (after ? in the URL), or slightly more advanced by doing an Ajax call from your JS code and make JS do the changes of the page that you desire. The latter is more complex and requires some knowledge of Javascript, but the page does not need to be reloaded in its entirety.
Did you mean something like this?
<script>
var foo_bar = {$foo.bar|escape:javascript};
</script>
Note that, as mentioned above, the value is computed server-side.
UPD. I get it now, you wanted to pass value the other way around. No, that’s not possible.

Categories