如何重置GET变量

时间:2012-05-27 22:42:48

标签: php

请原谅我的PHP noobness!我希望单个页面根据传入链接显示特定导航。它的设置如下:

page1.php list item:

<li><a <?php if (strpos($_SERVER['PHP_SELF'], 'wood-species.php')) echo 'class="current"';?>href="wood-species.php?name=standard">Wood Species</a></li>

page2.php列表项目:

<li><a <?php if (strpos($_SERVER['PHP_SELF'], 'wood-species.php')) echo 'class="current"';?>href="wood-species.php?name=premium">Wood Species</a></li>

因此,第一个链接将使用GET方法将name变量设置为“standard”,第二个链接将其设置为“premium”。所以我想要发生的是'wood-species.php'显示适当的导航,但我不确定如何破坏或取消设置'name'变量。

这是我试图使用的,显然没有完成它......

 <?php

    $woodclass = $_GET['name'];
    if($woodclass="premium") {
    require("includes/premium-wide-navigation.php"); 
    echo"premium";
    clearit();
    }

    else if($woodclass="standard") {
    require("includes/standard-wide-navigation.php");
    echo"standard";
    clearit();
    }
    else {
    //
    }

    function clearit () {
    unset($GLOBALS['woodclass']);
    }

?>

5 个答案:

答案 0 :(得分:4)

我认为未设置($ _ GET [&#39; name&#39;]);应该做的。

答案 1 :(得分:1)

您可以使用unset($myVar);简单地取消设置变量。

请在此处阅读:http://php.net/manual/en/function.unset.php

答案 2 :(得分:0)

使用javascript

形式:

<form method="get" action="<? print $_SERVER['PHP_SELF']; ?>"> <input type="text" name="number" size="5" maxlength="5">
<input type="submit" name="submit" value="Search"> <input id="refresh_custom_input" type="reset"> </form>

使用Javascript:

//refresh GET variable
$('#refresh_custom_input').click(function() {

    var query = window.location.search.substring(1)

    if(query.length) {
       if(window.history != undefined && window.history.pushState != undefined) {
            window.history.pushState({}, document.title, window.location.pathname);
       }
    } 

    window.location.reload(); 
});

答案 3 :(得分:0)

header("location: somewhere.php");将删除所有$ _GET变量。

答案 4 :(得分:0)

如果您正在处理条件并且只想清除来自 url 的任何 get 请求,您可以这样做

 if (!empty($_GET))
    header("Location: page.php");

这实际上只适用于条件 tho