从AJAX/Json设置PHP变量

从AJAX/Json设置PHP变量,php,javascript,jquery,ajax,json,Php,Javascript,Jquery,Ajax,Json,目前我有以下代码: index.php var refreshId = setInterval(function() { $.ajax({ url: ("http://www.example.co.uk/erc/user_data.php?imei="+inCallingNum), dataType: "json", //the return type data is jsonn success: function(data){ // <--- (

目前我有以下代码:

index.php

      var refreshId = setInterval(function() {

        $.ajax({
  url: ("http://www.example.co.uk/erc/user_data.php?imei="+inCallingNum),
  dataType: "json", //the return type data is jsonn
  success: function(data){ // <--- (data) is in json format
    $('#right-side').html(data.rightside);
        $('#caller').html(data.caller);
        $('.location').html(data.location);
        $('.battery-level').html(data.battery);
        //parse the json data
  }
});
    });
这可以很好地将信息添加到div标记中,但我现在需要做的是从user_data.PHP文件中获取一个PHP变量,并在index.PHP文件中使用它。可以用这种方式发送/捕获PHP变量吗

例如,在user_data.php中,我有一个变量$test,我想在index.php中使用这个$test变量


感谢您的帮助

因为index.php是在user_data.php之前执行的,所以您所请求的不能真正完成。但是,由于您正在将结果从user_data.php发送到index.php,因此您可以考虑使用JavaScript使用该变量。为什么不告诉我们更多关于您希望完成的内容,以便我们可以建议如何完成。

由于index.php是在user_data.php之前执行的,因此您要求的内容实际上无法完成。但是,由于您正在将结果从user_data.php发送到index.php,因此您可以考虑使用JavaScript使用该变量。为什么不告诉我们更多关于您想要完成的事情,这样我们就可以建议如何完成。

两种方法:

$test
设置为
$\u会话['test']
,然后在index.php上获取它

备选方案:

你试过两种方法吗

$test
设置为
$\u会话['test']
,然后在index.php上获取它

备选方案:


你是否尝试过使用

有很多方法可以做到这一点,最简单(也是最透明的方法)是设置会话cookie。这是一个位于客户端计算机上的小文件,您的子域(x.mydomain.com.)上的所有站点都可以读取,基本上所有文件都与设置它的文件位于同一文件夹中。通过执行以下操作,您可以在PHP中轻松实现这一点:

  • 在您想要设置、获取或检查变量的每个页面上。。。使用此代码

    session_start();  // Put this at the TOP of your page, below <? or just before you check the variables.
    
    $_SESSION['variable'] = "data";
    session_write_close();  // Use this after you are done setting the session data, to save it before the page execution is finished.  This is a good habit to get in to, it's kind of like when you fclose a file instead of waiting for the script to do it.
    
    $test = $_SESSION['variable'];
    
  • 在页面上,您希望获取变量。。使用此代码

    session_start();  // Put this at the TOP of your page, below <? or just before you check the variables.
    
    $_SESSION['variable'] = "data";
    session_write_close();  // Use this after you are done setting the session data, to save it before the page execution is finished.  This is a good habit to get in to, it's kind of like when you fclose a file instead of waiting for the script to do it.
    
    $test = $_SESSION['variable'];
    
  • 基本上,您可以使用$\u会话数组来存储希望在站点上显示为“全局”的变量。论坛使用它来存储用户ID和会话哈希,以便以后对密码进行身份验证。其他站点使用会话cookie在给定的时间范围内限制用户活动

    --

    还有另一种方法可以做到这一点,从您的页面中创建一个生成$test值的链接,并向index.php发送一个GET请求(例如,如果用户单击一个链接,则将该链接格式化为:

    index.php?test=value
    
    然后在index.php上只需执行以下操作:

    $test = $_GET{'test'];
    

    此方法适用于不支持cookie或禁用cookie的用户;但非常明显,用户可以轻松更改cookie的值(可能会有不可见的结果)。

    您可以使用多种方法来执行此操作,最简单(也是最透明的方法)是通过设置会话cookie。这是一个位于客户端计算机上的小文件,可以被子域(x.mydomain.com.)上的所有站点读取,基本上所有文件都与设置它的文件位于同一文件夹中。您可以通过执行以下操作在PHP中轻松完成此操作:

  • 在您想要设置、获取或检查变量的每个页面上…使用以下代码

    session_start();  // Put this at the TOP of your page, below <? or just before you check the variables.
    
    $_SESSION['variable'] = "data";
    session_write_close();  // Use this after you are done setting the session data, to save it before the page execution is finished.  This is a good habit to get in to, it's kind of like when you fclose a file instead of waiting for the script to do it.
    
    $test = $_SESSION['variable'];
    
  • 在您想要获取变量的页面上..使用以下代码

    session_start();  // Put this at the TOP of your page, below <? or just before you check the variables.
    
    $_SESSION['variable'] = "data";
    session_write_close();  // Use this after you are done setting the session data, to save it before the page execution is finished.  This is a good habit to get in to, it's kind of like when you fclose a file instead of waiting for the script to do it.
    
    $test = $_SESSION['variable'];
    
  • 基本上,您可以使用$\u会话数组来存储您希望在站点上被视为“全局”的变量。论坛使用它来存储用户ID和会话哈希,以便以后对密码进行身份验证。其他站点使用会话cookie来限制用户在给定时间范围内的活动

    --

    还有另一种方法可以做到这一点,从您的页面中创建一个生成$test值的链接,并向index.php发送一个GET请求(例如,如果用户单击一个链接,则将该链接格式化为:

    index.php?test=value
    
    然后在index.php上只需执行以下操作:

    $test = $_GET{'test'];
    

    此方法适用于不支持cookie或禁用cookie的用户;但非常明显,用户可以轻松更改cookie的值(可能会有看不见的结果)。

    您在问题中找到了问题的答案

    即使您正在创建一个数组并将其编码为JSON,实际上您只是在将值从php传递到javascript

    你也可以这样做

    $profile['test'] = $test;
    
    然后在javascript中使用

    data.test 
    
    为了得到你的价值


    问题是您希望何时获得变量值?是在页面提供之前还是在ajax调用期间?

    您在问题中找到了问题的答案

    即使您正在创建一个数组并将其编码为JSON,实际上您只是在将值从php传递到javascript

    你也可以这样做

    $profile['test'] = $test;
    
    然后在javascript中使用

    data.test 
    
    为了得到你的价值


    问题是您希望何时获得varaible值?是在页面提供之前还是在ajax调用期间?

    您应该使用PHP动态创建JavaScript代码:

    alert("server name: <?php
      echo $_SERVER['SERVER_NAME'];
    ?>");
    
    警报(“服务器名称:”);
    

    在极少数情况下,请谨慎使用。

    您应该使用PHP动态创建JavaScript代码:

    alert("server name: <?php
      echo $_SERVER['SERVER_NAME'];
    ?>");
    
    警报(“服务器名称:”);
    

    请谨慎使用,仅在极少数情况下使用。

    嗨,cbroughton,谢谢你的帮助。我会尝试一下,让你知道我的进展:)效果很好,但我现在有另一个问题,所以我将发表一篇新文章。再次感谢你的帮助:)嗨,cbroughton,谢谢你的帮助。我会尝试一下,让你知道我是怎么做到的:)效果很好,但我现在有另一个问题,所以我会写一篇新文章。再次感谢您的帮助:)