user360907
user360907

Reputation:

Passing variables from HTML to PHP using form submission

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>

Upvotes: 0

Views: 1647

Answers (3)

Felix Geenen
Felix Geenen

Reputation: 2707

@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

Upvotes: 2

Boris Gu&#233;ry
Boris Gu&#233;ry

Reputation: 47585

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'];

Upvotes: 1

JCOC611
JCOC611

Reputation: 19719

You need to access the superglobal $_GET:

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

Upvotes: 1

Related Questions