0

I'm having a problem with the following piece of code. My desire is to have the heading Search results appear when the form is submitted using the 'Search' button, and I'm trying to implement this using the hidden input called searching. The idea is that when the form is submitted, this value is set to 'yes', and that will reveal the heading, but that is not what is happening here. Can anyone please tell me where I've gone wrong?

<html>
    <head>
        <title>Search</title>
    </head>

    <body>
        <h1>Search</h1>

        <form name = "search" action = "<?=$PHP_SELF?>" method = "get">
            Search for <input type = "text" name = "find" />
            <input type = "hidden" name = "searching" value = "yes" />
            <input type = "submit" name = "search" value = "Search" />
        </form>

        <?php
            if ($searching == "yes")
            {
                echo "<h2>Search results</h2>";
            }
        ?>
    </body>
</html>

3 Answers 3

2

@chris, you dont have to use a hidden field. you just can check if the form was submitted like this:

if(isset($_GET['search'])) echo 'foo';

@Boris, why should it be more secure to store the global into another var? I would agree if you check the global against a regex or whatever before.

Felix

Sign up to request clarification or add additional context in comments.

1 Comment

In old version of PHP, all variables from GET and POST were available as global (register_global=on), it was about to show that you can't use $searching directly but you needed to get it from the $_GET predefined variable. You obviously don't need to do that
1

You need to access the superglobal $_GET:

if($_GET["searching"]=="yes"){
  //echo here
}

1 Comment

did you ever try to program under E_ALL?
1

Unless you're using an old version of PHP, or a really unsecure configure, you're likely not using global variables.

Therefore, you need to first retrieve your $searching variable from the magic $_GET variable.

$searching = $_GET['searching'];

1 Comment

did you ever try to program under E_ALL?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.