dqrqp8492
2011-06-03 09:20
浏览 46
已采纳

可以将带有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!

图片转代码服务由CSDN问答提供 功能建议

可能重复:
将javascript变量传递给PHP n

您好

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

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

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

谢谢! < / DIV>

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • dougong5285 2011-06-03 09:24
    已采纳

    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'] : "";
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • douben7260 2011-06-03 09:23

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

    评论
    解决 无用
    打赏 举报
  • duanlei0282 2011-06-03 09:23

    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' }
    });
    
    评论
    解决 无用
    打赏 举报
  • dotxxh0998 2011-06-03 09:23

    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.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题