简体   繁体   English

在装有jQuery的PHP模块之间传递变量

[英]Passing variables between PHP modules loaded with jQuery

I'm using jQuery to load a PHP module, in "deferred" mode, however the jQuery loaded module cannot access variables of the "main" PHP module. 我使用jQuery以“延迟”模式加载PHP模块,但是jQuery加载的模块无法访问“主要” PHP模块的变量。 It looks like it can't access session variables too. 看起来它也无法访问会话变量。 Is there a simple, quick and safe way to achieve this? 有没有简单,快速和安全的方法来实现这一目标?

This is the code used to load the "deferred" page: 这是用于加载“延迟”页面的代码:

<script type="text/javascript">
    jQuery.noConflict();
    jQuery("#sidebarfeat").one('inview', function(event, isInView, visiblePartX, visiblePartY) {
        if (isInView) {
            // element is now visible in the viewport
            jQuery("#sidebarfeat").load("/wp-content/themes/tgv1/featured.php", "",
            function(responseText, textStatus, XMLHttpRequest) {
                if(textStatus == 'error') {
                    jQuery('#sidebarfeat').html('There was an error making the AJAX request');
                }});

          }
        });

</script>

The code above uses jQuery inview plugin, however the problem lies into the PHP environment, it looks like the 2 PHP modules are running in "separated" environments.... 上面的代码使用jQuery inview插件,但是问题出在PHP环境中,看起来2个PHP模块都在“分离的”环境中运行。

PHP doesn't keep state between multiple requests. PHP不会在多个请求之间保持状态。 This means that standalone PHP cannot store variables in memory between requests. 这意味着独立的PHP无法在两次请求之间将变量存储在内存中。 Sessions are saved to disk (by default), config files needs to be reparsed, etc. 会话会保存到磁盘(默认情况下),配置文件需要重新解析等。

If you want to keep variables between the two requests, you can use sessions , or you can json_encode() your variables and pass them again through jQuery. 如果要在两个请求之间保留变量,则可以使用session ,也可以使用json_encode()将变量再次通过jQuery传递。

Server-wise, your XHR request is the same as if you requested the page in your browser. 在服务器端,您的XHR请求与在浏览器中请求该页面相同。 It's a completely different request. 这是完全不同的要求。

As for not being able to access session variables, you need to make sure that you call session_start() (with possibly the proper session_name() call before, look at your cookies to figure that one out) in your fetched script as well. 至于无法访问会话变量,则还需要确保在提取的脚本中也调用了session_start() (可能之前调用了适当的session_name() ,请查看cookie以弄清楚这一点)。


EDIT: Using json_encode() is relatively easy. 编辑:使用json_encode()相对容易。 First, prepare all your variables you want to pass into a associative array, then call json_encode() . 首先,准备要传递到关联数组中的所有变量,然后调用json_encode()

$myVars = array();
$myVars['important'] = 'very';

$myJsVars = json_encode($myVars);

Then, in your <head> , assign $myJsVar to a JavaScript variable. 然后,在<head> ,将$myJsVar分配给JavaScript变量。

<script>var PHP_VARS = <?php echo $myJsVars; ?>;</script>

When you call .load in jQuery, pass in your variable as the second parameter. 当您在jQuery中调用.load时,将您的变量作为第二个参数传递。

<script type="text/javascript">
    jQuery.noConflict();
    jQuery("#sidebarfeat").one('inview', function(event, isInView, visiblePartX, visiblePartY) {
        if (isInView) {
            // element is now visible in the viewport
            jQuery("#sidebarfeat").load("/wp-content/themes/tgv1/featured.php", PHP_VARS,
            function(responseText, textStatus, XMLHttpRequest) {
                if(textStatus == 'error') {
                    jQuery('#sidebarfeat').html('There was an error making the AJAX request');
                }});

          }
        });

</script>

featured.php will now be able to access those variables via $_GET . featured.php现在将可以通过$_GET访问这些变量。

You could try to pass your session_id() to the AJAX called script. 您可以尝试将session_id()传递给称为脚本的AJAX。 Of course current variables will never be available in another process. 当然,当前变量永远不会在另一个进程中可用。

You could always pass the relevant data through the data: parameter of the .load() method. 您总是可以通过.load()方法的data:参数传递相关数据。 Simply put the values into an array and serialize the array to a string. 只需将值放入数组并将数组序列化为字符串即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM