passing variable javascript into php [duplicate] - php

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/

Related

PHP to HTML 5 using Javascript

I have a difficult problem, we are currently using PHP on our server and I am a PHP programmer. I have built a site connected to our PHP site that is completely client side HTML 5 and Javascript. I can pass variables from HTML 5 to PHP but I want to import some of the data from the PHP to the HTML pages such as session ...etc. how can I pass PHP sessions to Javascript on another page or access the session data to be used on HTML 5 and Javascript?
The main reason for this is for users with low bandwidth and for the ability of HTML 5 to use local storage and offline capabilities if their line is disconnected so they can post the data at a later time to the PHP pages.
Help would be great?
I hope I am not comepletely wrong here, but what you are essentially trying to do is writing data from php to javascript. You could use AJAX as akluth suggested, but I think it is easier to just do the following in the javascript;
data = <?php echo $foo; ?>;
You should however be careful when saving the session in localstorage since it can be modified by the client. Probably double check once the user attempts to post the data later. See this SO answer for more details on that.
If you want to get session data in PHP you can use following segment
<script>var $_SESSION = <?= json_encode($_SERVER) ?></script>
If some data to be maintained on different pages and not require sever data, better you use Session Storage of HTML5 [http://www.xul.fr/en/html5/sessionstorage.php]
This will reduce server load + data transfer and increase speed of your application
HTML5 local storage is not fully supported so some thing that can work anywhere can be do in following way:
First have your PHP Session into JS var:
<script type="text/javascript">
var sessionData= <?php echo json_encode($_SESSION); ?>
</script>
Then you will have session data in sessionData variable in JSON format. then if you want to use it on different places then simply set and fetch it into cookies via Javascript.
Please tell if some thing is still unclear.

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.

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

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.

How do I assign javascript variable to php variable? With out submitting the form

hi friends can some one tell me How do I assign javascript variable to php variable?
I am actually getting a value through javascript and I want the javascript values to be assigned to php variables. With out submitting the form.
Unless you are doing something really esoteric (such as running server side JS and PHP):
The PHP runs, generates a response, sends it to the browser. If that response includes JavaScript then that JS will then be executed.
You can't get data from the JS back to the PHP without issuing a new HTTP request.
This could be done by setting document.location, adding an <img> element to the document with the data passed via the src attribute (in both cases including the data in a query string, posting a <form> to an <iframe>, using an XMLHttpRequest object and a host of other methods.
It really depends on what you want to achieve.
You don't, directly.
You have to use AJAX, but beware that is far more complicated than "just assigning it".
Use the jQuery selector to do that. OR use a hidden form if you don't want the user to see it.
For example, do this.
<input type="hidden" name="YOUR_OWN_UNIQUE_NAME" id="UNIQUE_ID" value >
After that, just use jQuery selector
$("#UNIQUE_ID").val("VALUE")
That's about it, not much problems!
You need to send an AJAX request containing the information to a separate PHP script that processes the data.
You can assign PHP variable to a JavaScript one:
var some = <?php echo $other; ?>
But you can't do this backwards - it is a matter of that php is run BEFORE javascript. ;]
Best way will be to send the javascript variable value to the server asynchronously before the form is submitted using AJAX, but be aware that you will only be able to bring back a variable or store the data server side via a db or text file.
what do you want to do with the data that is sent to the server?

Categories