Possible to pass a variable from jQuery with AJAX to PHP page and a session variable? [duplicate]
Possible Duplicate:
Passing javascript variable to PHP
Hi
I wonder if it's possible to pass a variable from a jQuery script to a PHP-page and put the variable into a session variable like this:
$_SESSION['mapZoomArea'] = (isset($_GET['mapza']) ? $_GET['mapza'] : "";
I'm not sure how to pass the variable and the url to the server? Preciate some help!
Thanks!
If you want a dedicated service specifically for writing this value into the session, you probably should make it a POST request (GET will work too, but GET requests should be for data retrieval, not for writing to the server).
Therefore, simply create a new PHP page, let's say "storezoomarea.php", and have jQuery make an Ajax POST request to that page:
$.ajax({url: "storezoomarea.php", type: "post", data: {"mapza": mapza}})
Then, on the server side, you can retrieve it from the _POST variable:
$_SESSION['mapZoomArea'] = (isset($_POST['mapza']) ? $_POST['mapza'] : "";
Not bad. Make sure you've called session_start();
first. Pass the variable in as a query string, 'http://whatever.com/?mapza=yourvariablevaluehere'. You can do this with jQuery by:
$.ajax({
url : 'urlhere',
data : { mapza : 'your variable value here' }
});
Ok.
Yes, you can pass that var to a php code, if you make an AJAX call, with jquery ($.Ajax(whatever)), and of course, in the file called with AJAX change the session var.
Hi you should use AJAX. Since you have JQuery available it is very simple.
More reading available here http://api.jquery.com/jQuery.ajax/
Example:
$.ajax({
type: "GET",
url: "some.php",
data: ({'mapza' : yourvariable}),
success: function(msg){
alert( "Data Saved: " + msg );
}
});
HTH :)
精彩评论