RE: Hidden Fields

liunx

Guest
I have a seemingly basic question, but I'm having a hard time understanding hidden fields.<br />
<br />
I understand the concept of hidden fields, but it their use that confuses me. For instance, if I have a multi-page form, I would use hidden fields to carry input to the following form page. But how do I do that? <br />
<br />
What goes in the value section of the syntax, i.e <br />
<input type="hidden" name="select" value=" ?"><br />
<br />
And finally, do I need a hidden field for each form element, or does one hidden field capture all information on the page?<br />
<br />
Any help is appreciated...thanks<br />
<br />
t~<!--content-->A hidden field is just like any other types of input, except it has just the value you assign to it instead of getting the visitor to enter some information for that specific field.<br />
<br />
One good use for the hidden field is... if you have many different pages with forms on them, the HIDDEN field can be used to say which page the information is coming from.<br />
<br />
<input type="hidden" value="From contact page"><br />
or <br />
<input type="hidden" value="Advertising page"><br />
or<br />
<input type="hidden" value="My secret page"><br />
<br />
It is just basically something to pass on a static value to the end result.<!--content-->htmlite<br />
<br />
So, the "value" of a hidden field is something I input, and not information taken from user input?<br />
<br />
How then does the user input go from page 1 of the form, to page 2?<!--content-->Hidden fields are often used by server side scripts but not extremely valuable for pages not using server side scripts. Instead you can use the location bar (e.g. search strings) to pass data between pages.<br />
<br />
Example:<br />
<br />
<a href=http://www.htmlforums.com/archive/index.php/"page2.html?test=5">5</a><br />
<a href=http://www.htmlforums.com/archive/index.php/"page2.html?test=cow">cow</a><br />
<a href=http://www.htmlforums.com/archive/index.php/"page2.html?test=hand">hand</a><br />
<br />
In page2:<br />
<br />
<script><br />
var sea = window.location.search;<br />
var ind = sea.indexOf("?test=")+6;<br />
alert("you pressed: "+sea.substring(ind));<br />
</script><!--content-->Originally posted by tp194 <br />
htmlite<br />
<br />
So, the "value" of a hidden field is something I input, and not information taken from user input?<br />
<br />
How then does the user input go from page 1 of the form, to page 2? <br />
<br />
<br />
depending on the action="" part of your form tag, the form:<br />
<br />
action="get": passed in the url as a querystring. Looks like: <!-- m --><a class="postlink" href="http://yourpage.html?name=Dr.Web&level=moderator">http://yourpage.html?name=Dr.Web&level=moderator</a><!-- m --><br />
<br />
action="post": passed in the document body. <br />
<br />
You can use javascript to extract the form contents, but usually you would use a server side page to get the contents out. Usually you don't go from one form page to the next while passing the values.... you would 'use' the values... maybe store them in a database or something, then move on.<!--content-->So, I have some sample code, that I'm hoping you guys can explain to me what the hidden field does: <br />
<br />
(by the way...thanks for the help!) <br />
<br />
<?php<br />
$num_to_guess = 42;<br />
$num_tries = ( isset( $num_tries ) ) ? ++$num_tries : 0;<br />
$message = "";<br />
if ( ! isset( $guess ) )<br />
$message = "Welcome to the guessing machine!";<br />
elseif ( $guess > $num_to_guess )<br />
$message = "$guess is too big! Try a smaller number";<br />
elseif ( $guess < $num_to_guess )<br />
$message = "$guess is too small! Try a larger number";<br />
else // must be equivalent<br />
$message = "Well done!";<br />
<br />
$guess = (int) $guess;<br />
?><br />
<br />
<html><br />
<head><br />
<title>Listing 9.10 Saving state with a hidden field</title><br />
</head><br />
<body><br />
<h1><br />
<?php print $message ?><br />
</h1><br />
Guess number: <?php print $num_tries?><br />
<form method="POST"><br />
Type your guess here:<br />
<input type="text" name="guess" value="<?php print $guess?>"><br />
<input type="hidden" name="num_tries" value="<?php print $num_tries?>"><br />
</form><br />
</body><br />
</html><!--content-->
 
Back
Top