可以将带有AJAX的jQuery变量传递给PHP页面和会话变量吗? [重复]

可以将带有AJAX的jQuery变量传递给PHP页面和会话变量吗?  [重复]

问题描述:

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!

可能重复: strong>
将javascript变量传递给PHP p> blockquote> n

您好 p>

我想知道是否可以将变量从jQuery脚本传递到PHP页面并将变量放入会话变量中: p> \ n

$ _SESSION ['mapZoomArea'] =(isset($ _ GET ['mapza'])?$ _GET ['mapza']:“”; code> p>

我不确定如何将变量和url传递给服务器?提供一些帮助! p>

谢谢! p>

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'] : "";

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 :)

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.