PHP:使用过滤器构建搜索时未定义的索引错误

时间:2013-07-25 19:46:31

标签: php search filter

//错误:信息被附加但未被替换。

<form action="testme.php" method="get">
    <p>
        <input name="search" type="text" value="">
        <input type="submit" name="button" id="button" value="Submit">
    </p>

    <h3>Filter Results</h3>
<p><a href="testme.php?filter='Action'<?php if (isset($_GET['search']) && !empty($_GET['search'])) {echo '&search='.$_GET['search'];}
?>">Action</a></p>


<p><a href="testme.php?filter='Comedy'<?php if (isset($_GET['search']) && !empty($_GET['search'])) {echo '&search='.$_GET['search'];}
?>">Comedy</a></p>

</form>


<?php

if (isset($_GET['button']) && isset($_GET['search']) != '') {
    echo $_GET['search'];

}
elseif (isset($_GET['filter']) == "Action" && isset($_GET['search']) != '') {
    echo "Action " . $_GET['search'];

}

elseif (isset($_GET['filter']) == "Action" && isset($_GET['search']) == '') {

    echo " No action Involved ";
}

if (isset($_GET['filter']) == "Comedy" && isset($_GET['search']) == '') {

    echo " No Comedy Involved";
}

//当我点击链接中的任何一个时我得到:没有动作参与没有喜剧参与。但我只想要一个人展示     ?&GT;

4 个答案:

答案 0 :(得分:0)

因为$_GET['search']未定义

正确的代码:

 <form action="index.php" method="get">
    <p>
        <input name="search" type="text" value="">
        <input type="submit" name="button" id="button" value="Submit">
    </p>



    <h3>Filter Results</h3>
    <p>
    <a href="index.php?filter='Action'&search=<?php if (isset($_GET['search'])) { echo $_GET['search']; } ?>">Action</a>
    </p>
</form>


    <?php

if (isset($_GET['button']) && isset($_GET['search']) != '') {
    echo $_GET['search'];

}
elseif (isset($_GET['filter']) == "Action" && isset($_GET['search']) != '') {
    echo "Action " . $_GET['search'];

}
//the logic is to click on the action without filling in the search form and get it to work

elseif (isset($_GET['filter']) == "Action" && empty($_GET['search'])) {

    echo "No action Involved";
}
?>

答案 1 :(得分:0)

PHP在页面发送到浏览器之前运行,而不是之后。因此,当首次发送页面时,构建链接的代码尚无价值。

答案 2 :(得分:0)

你必须在定义GET ['search']之前,然后声明这个<a href="index.php?filter='Action'&search=<?php echo $_GET['search']

答案 3 :(得分:0)

这应该有效

<a href="index.php?filter='Action'&search=<?=@$_GET['search']; ?>>