开发者

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. 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....


PHP doesn't keep state between multiple requests. This means that standalone PHP cannot store variables in memory between requests. 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.

Server-wise, your XHR request is the same as if you requested the page in your browser. 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.


EDIT: Using json_encode() is relatively easy. First, prepare all your variables you want to pass into a associative array, then call json_encode().

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

$myJsVars = json_encode($myVars);

Then, in your <head>, assign $myJsVar to a JavaScript variable.

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

When you call .load in jQuery, pass in your variable as the second parameter.

<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.


You could try to pass your session_id() to the AJAX called script. 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. Simply put the values into an array and serialize the array to a string.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜