简体   繁体   English

使用PHP可读的哈希导航时,如何在URL中添加参数?

[英]How can I add parameters to the url when using hash navigation which is readable by PHP?

I have written a single page application in HTML5. 我已经用HTML5编写了一个页面应用程序。 I use JQuery and hash navigation to navigate between pages. 我使用JQuery和哈希导航在页面之间导航。 This works fine so far. 到目前为止,这个工作正常。 Now I have written a really simple php script that pulls blog entries from my database. 现在,我编写了一个非常简单的PHP脚本,该脚本从数据库中提取博客条目。 Of course I want the user to be able to link those articles and navigate tags etc. How would I implement that? 当然,我希望用户能够链接这些文章并浏览标签等。我将如何实现呢?

I tried http://example.com/#blog?tag=news but somehow my php script is unable to catch that parameter. 我尝试了http://example.com/#blog?tag=news,但是以某种方式我的php脚本无法捕获该参数。

That is a fragment denoted by the # and is only used by the browser and not passed to the server. 这是一个由#表示的片段,仅由浏览器使用,而不传递给服务器。 PHP will not receive $_GET['tag'] and not #blog as you have it constructed. PHP不会像您构造的那样收到$_GET['tag']而不是#blog

Try ?tag=news#blog but PHP will still not get the #blog . 尝试?tag=news#blog但是PHP仍然不会获得#blog

我认为它看起来像http://www.example.com?tag=news#blog看起来更好,而且@AbraCadaver指出的#仅由浏览器使用。

<?php echo $_GET['tag']; ?> //would output news

您可以使用此变量$ _GET ['tag']进行捕捉

you must use java scritp eg: 您必须使用java scritp,例如:

var hash = location.hash; var hash = location.hash;
it will return the hash value to you 它将哈希值返回给您
but you will need send to server-side using 但您需要使用发送到服务器端
the post or get of java script Java脚本的发布或获取

I have solved the problem and wanted to share here how. 我已经解决了这个问题,并想在这里分享如何。

The underlying problem here was the loading of all content with JQuery. 这里的根本问题是使用JQuery加载所有内容。 While my initial URI was wrong, http://example.com/?tag=news#blog did not solve it either. 虽然我的初始URI错误,但http://example.com/?tag=news#blog也无法解决。 The variable was never sent to the $_GET Array of PHP, because the index.html does not contain any PHP and the script is loaded into a div via JQuery/Ajax. 该变量从未发送到PHP的$ _GET数组,因为index.html不包含任何PHP,并且脚本通过JQuery / Ajax加载到div中。

So the solution here was to modify my JQuery Function: 因此,这里的解决方案是修改我的JQuery函数:

function navigate(theHash)
{
    var parsed = theHash;
    var tag    = '';

    if ( theHash.indexOf("/") != -1 ){ /* Check if there is a trailing slash in the string */
        parsed = theHash.substr(0, theHash.indexOf("/"));
    }
    if ( theHash.indexOf(",") != -1 ){ /* Check for Parameters to send to PHP */
        parsed = theHash.substr(0, theHash.indexOf(","));
        tag = theHash.substr(theHash.indexOf(','), theHash.length);
    }


    if ( parsed.localeCompare("#!portfolio") == 0 ){
        $('#content').load('portfolio.html');
    }
    else if ( parsed.localeCompare("#!blog") == 0 ){
        if ( tag.localeCompare('') != 0 )
        {
            $("#content").load('blog.html?tag='+tag.substr(1, tag.length));
        }
        else{
            $("#content").load('blog.html');        
        }
    }
    else{
        $('#content').load('front.html');
    }
}

So with the little change in the blog part of the if statement to load the html page with the parameter appended (which contains the php code), the variable gets transported correctly to PHP and I can read it via $_GET. 因此,如果if语句的博客部分进行了少许更改以加载带有附加参数的html页面(其中包含php代码),则该变量将正确传输到PHP,我可以通过$ _GET进行读取。 While this is only a first draft and needs to get refined for more parameters and more robust behavior, it shows how I solved the issue. 尽管这只是初稿,并且需要完善以获取更多参数和更可靠的行为,但它显示了我是如何解决此问题的。

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

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