Chapter 7 of Build your own Database Driven Web Site

Hi everyone,

On page 219 and 220 of the book “Build your own Database Driven Web Site using php & MySQL”, it shows the code for a form for the searchform.html.php template:

I noticed that part of the code in the form was as follows:

<div>
<input type=“hidden” name=“action” value=“search”/>
<input type=“submit” value=“Search”/>
</div>

The above shows an input button as well as a hidden field.

In the controller file, it detects the search button is clicked by using the following code:

if (isset($_GET[‘action’]) and $_GET[‘action’] == ‘search’)
{
include $_SERVER[‘DOCUMENT_ROOT’] . ‘/includes/db.inc.php’;
….etc.

I know that since the name attribute in the hidden field is set to “action”, then the if statement in the abovementioned code checks to see if $_GET[‘action’] is set, and if so, it then checks to see if its value is ‘search’.

I don’t understand though why the hidden field is needed here. Since the input element for the submit button already contains a value of “search”, couldn’t you just have the one submit button and then put the name attribute and its value in that input element, ie.

<input type=“submit” name=“action” value=“Search”/>

Then the above if statement would be checking the exact same thing wouldn’t it?

I’m just can’t see what the hidden field is for and wondered if someone could clarify this so it’s understood?

Appreciate any help.

Inconsistency between old browsers. Not all send the value of the button if the button isn’t clicked, for example when submitting the form by pressing enter while your mouse is in another form field.