繁体   English   中英

加载AJAX的页面给出了空的$ _GET

[英]AJAX-loaded page gives empty $_GET

通过AJAX方法加载的页面。 index.php是基础页面,profile.php是已加载页面

jQuery :(“发布”与HTML请求无关)

$('<a/>', {href: '?userID='+post['userID']+'#profile'}).text(post['firstName']+' '+post['lastName'])

HTML:

<a href="?userID=1#profile">Firstname Lastname</a>

原始网址(点击后)

http://########/#####/index.php?userID=1#home

$ _GET print_r在profile.php上:

Array ()

按要求; Ajax加载javascript(index.php):

    //AJAX page loading

        $(document).ready(function(){

            //Default page
            if(!window.location.hash)
                window.location.hash = '#home';

            //Check page reference
            checkURL();

            //Update nav
            $('#main-nav li').on("click", function (){
                $('#main-nav li').removeClass('active');
                $(this).addClass('active');

                //Assign each link a new onclick event, using their own hash as a parameter
                checkURL(this.hash);    
            });

            //check for a change in the URL every 250 ms to detect if the history buttons have been used
            setInterval("checkURL()",250);  
        });

        //Store the current URL hash
        var lasturl=""; 

        function checkURL(hash){
            if(!hash)

                //if no parameter is provided, use the hash value from the current address
                hash=window.location.hash;
            if(hash != lasturl) { 

                //If hash changed, update current hash and load new one
                lasturl=hash;
                loadPage(hash);
            }

        }

        function loadPage(url) {

            //Adjust page name
            url=url.replace('#','');   
            url=url+'.php';

            //AJAX load page contents in to main content div
            $('#content').load(url);
        }

解:

  1. 在根页面(index.php)上创建全局存储。
  2. 在存储中设置变量,而不要使用$ _GET。
  3. 从子页面访问var。

例:

index.php(Javascript)

if (typeof(window.storage) === 'undefined') {
    window.storagePV = {};
}

$('#nav-profile').click(function() {
    window.storage.userID = <?php echo "$_SESSION[userID]"; ?>;
});

访问userID以查看用户的个人资料页面

var userID = window.storage.userID

暂无
暂无

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

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