Multiple Forms
Moderator: General Moderators
-
pistolfire99
- Forum Commoner
- Posts: 58
- Joined: Thu May 23, 2002 3:18 pm
Multiple Forms
Hello All,
Here is a dilema. I have 3 forms. I am comfortable passing variables and requesting them onto another page and so on. The thing I am trying to work out is. After someone fills in the first form, I display certain of that information onto the next page while rest of them($variables) is 'hidden'. Now that form1 is bulky, in terms of the total number of fields, I dont want to make the form2 very clumsy by creating all those 'hidden' fields to just hold the variables. Then when the user submits the form2, that information goes to form3 and then it gets submitted to the database.
Now the thing that is beyond me is, is their an alternative to creating hidden fields for all those variables. Is their anyway in PHP where I can request the whole form array and store in just One hidden field and pass that information to form3, avoiding all that clumsy code. I guess you guys understand what I am trying to do here.... Any suggestions would be appreciated.
Here is a dilema. I have 3 forms. I am comfortable passing variables and requesting them onto another page and so on. The thing I am trying to work out is. After someone fills in the first form, I display certain of that information onto the next page while rest of them($variables) is 'hidden'. Now that form1 is bulky, in terms of the total number of fields, I dont want to make the form2 very clumsy by creating all those 'hidden' fields to just hold the variables. Then when the user submits the form2, that information goes to form3 and then it gets submitted to the database.
Now the thing that is beyond me is, is their an alternative to creating hidden fields for all those variables. Is their anyway in PHP where I can request the whole form array and store in just One hidden field and pass that information to form3, avoiding all that clumsy code. I guess you guys understand what I am trying to do here.... Any suggestions would be appreciated.
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
You can read up on sessions here:
http://www.php.net/manual/en/ref.session.php
If you are using PHP 4.1 or above you shouldn't need to use session_register() (and if you have register globals off it won't work). You'll be wanting instead to work directly with the $_SESSION array.
Mac
http://www.php.net/manual/en/ref.session.php
If you are using PHP 4.1 or above you shouldn't need to use session_register() (and if you have register globals off it won't work). You'll be wanting instead to work directly with the $_SESSION array.
Mac
why use sessions?
doesn't using sessions create more load on the server 
Yes sessions creates slightly more load on the server than not using them, but it gives you the developer the added convience of being able to propagate state variables in a relatively painless way. It also makes it harder for the user to manually change these values as they are no longer sent to the user. This means your scripts are more secure. In order to acheive the same level of protection you would have to do some rather involved mac'ing in the hidden form method that would probably make just about as much additional load as the sessions do....
-
pistolfire99
- Forum Commoner
- Posts: 58
- Joined: Thu May 23, 2002 3:18 pm
Hello Guys/Girls,
#1) I would like to thank you all for the nice replies.
#2) Below here is a sample code. Could someone please show me how to implement the $_SESSION array to grab the whole form rather than passing each individual items as hidden fields.
This is the first form where I pass the values to another page, where there is another form.
Here I have added the items of form1 as hidden elements, but I have more then15-20 $variables and dont want to create 15-20 hidden fields. As some of you pointed out by using $_SESSION array. Could someone plz show an example as to how would I implement it. I have general knowledge of using sessions.
Finally, this would be the last page where the information would get submitted to the Database.
Thank You,
#1) I would like to thank you all for the nice replies.
#2) Below here is a sample code. Could someone please show me how to implement the $_SESSION array to grab the whole form rather than passing each individual items as hidden fields.
This is the first form where I pass the values to another page, where there is another form.
Code: Select all
//temp.html
<HTML>
<HEAD></HEAD>
<BODY>
<FORM METHOD=POST ACTION="temp1.php">
Who is your favourite author?
<INPUT NAME="Author" TYPE="TEXT">
<BR>
<BR>
What is your favourite book?
<INPUT NAME="Book" TYPE="TEXT">
<BR>
<BR>
<INPUT TYPE="SUBMIT" VALUE="Submit">
<INPUT TYPE="RESET" VALUE="Reset">
</FORM>
</BODY>
</HTML>Code: Select all
//temp1.php
<HTML>
<HEAD></HEAD>
<BODY>
<FORM METHOD=POST ACTION="temp2.php">
Who is your favourite actor?
<INPUT NAME="Actor" TYPE="TEXT">
<BR>
<BR>
What is your favourite President?
<INPUT NAME="President" TYPE="TEXT">
<BR>
<BR>
<INPUT NAME="Author" TYPE="HIDDEN" VALUE="<?php echo $_POSTї'Author']; ?>">
<INPUT NAME="Book" TYPE="HIDDEN" VALUE="<?php echo $_POSTї'Book']; ?>">
<BR>
<BR>
<INPUT TYPE="SUBMIT">
<input name="BUTTON" type="BUTTON" value="Go Back" ONCLICK="history.go(-1)">
</FORM>
</BODY>
</HTML>Code: Select all
//temp2.php
<HTML>
<HEAD></HEAD>
<BODY>
Your favorite author is:
<B>
<?php echo $_POSTї'Author']; ?>
</B>
<BR>
<BR>
Your favorite book is:
<B>
<? echo $_POSTї'Book']; ?>
</B>
<BR>
<BR>
Your favorite actor is:
<B>
<? echo $_POSTї'Actor']; ?>
</B>
<BR>
<BR>
Your favorite president is:
<B>
<? echo $_POSTї'President']; ?>
</B>
<BR>
<BR>
<DIV ALIGN="CENTER">
<a href="./temp.html">click here</a>
</BODY>
</HTML>Code: Select all
<!-- temp.html -->
<html>
<head>
<title>Temp.html</title
</head>
<body>
<form method="POST" action="temp1.php">
Who is your favourite author?
<input name="author" type="text">
<br><br>
What is your favourite book?
<input name="book" type="text">
<br><br>
<input type="submit" value="Submit">
<input type="reset" value="Reset">
</form>
</body>
</html>Code: Select all
<?php
$author = addslashes($_POSTї'author']);
$book = addslashes($_POSTї'book']);
$_SESSIONї'author'] = $author;
$_SESSIONї'book'] = $book;
?>
<html>
<head>
<title>Temp.html</title
</head>
<body>
<form method="POST" action="temp2.php">
Who is your favourite actor?
<input name="actor" type="text">
<br><br>
Who is your favourite President?
<input name="president" type="text">
<br><br>
<input type="submit" value="Submit">
<input type="button" value="Fo Back" onClick="history.go(-1)">
</form>
</body>
</html>Code: Select all
//temp2.php
<html>
<head>
<title>temp2.php</title>
</head>
<body>
Your favorite author is:
<span style="font-weight: bold;">
<?php echo $_SESSIONї'author']; ?>
</span>
<br><br>
Your favorite book is:
<span style="font-weight: bold;">
<?php echo $_SESSIONї'book']; ?>
</span>
<br><br>
Your favorite actor is:
<span style="font-weight: bold;">
<?php echo $_POSTї'actor']; ?>
</span>
<br><br> Your favorite president is:
<span style="font-weight: bold;">
<?php echo $_POSTї'president']; ?>
</span>
<br><br>
<div align="center">
<a href="./temp.html">click here</a>
</div>
</body>
</html>Re: why use sessions?
carrying the whole data all forms long is overhead, too,isn't it?lanlord wrote:doesn't using sessions create more load on the server
just out of curiosity: why not ask for all those entries in one form? anyway.
Code: Select all
<?php
$hashes = array('Author', 'Book', 'Actor', 'President'); // mandatory fields
$missing = 0;
$current = 1;
foreach($hashes as $hash)
{
if (isset($_POSTї$hash]))
$_SESSIONї$hash]=$_POSTї$hash];
else if(!isset($_SESSIONї$hash]))
$missing |= $current;
$current *= 2;
}
// if $missing is == 0 all fields have been gathered
// if $missing != 0 it points to the missing fields
// i.e. $missing == 5 --> the first an the third element (2^0 + 2^2 = 1+4 = 5)
// so you can decide in an if-else-statement which form or output to show
?>