Multiple Forms

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
pistolfire99
Forum Commoner
Posts: 58
Joined: Thu May 23, 2002 3:18 pm

Multiple Forms

Post by pistolfire99 »

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.
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

Use sessions. Functions:-

session_register()
session_start()
session_unregistered()
session_destroy(
.
.
.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

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
User avatar
lanlord
Forum Newbie
Posts: 13
Joined: Wed Sep 18, 2002 10:02 am

why use sessions?

Post by lanlord »

doesn't using sessions create more load on the server :?:
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

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

Post by pistolfire99 »

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.

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>
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.

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&#1111;'Author'];  ?>">

<INPUT NAME="Book" TYPE="HIDDEN" VALUE="<?php  echo $_POST&#1111;'Book'];  ?>">

<BR>
<BR>

<INPUT TYPE="SUBMIT">
<input name="BUTTON" type="BUTTON" value="Go Back" ONCLICK="history.go(-1)">

</FORM>

</BODY>
</HTML>
Finally, this would be the last page where the information would get submitted to the Database.

Code: Select all

//temp2.php
<HTML>
	<HEAD></HEAD>

<BODY>
Your favorite author is:
<B>
<?php echo $_POST&#1111;'Author']; ?>
</B>

<BR>
<BR>

Your favorite book is:
<B>
<?	echo $_POST&#1111;'Book']; ?>
</B>

<BR>
<BR>

Your favorite actor is:
<B>
<?	echo $_POST&#1111;'Actor']; ?>
</B>

<BR>
<BR>

Your favorite president is:
<B>
<?	echo $_POST&#1111;'President']; ?>
</B>

<BR>
<BR>

<DIV ALIGN="CENTER">
<a href="./temp.html">click here</a>

</BODY>
</HTML>
Thank You,
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

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&#1111;'author']);
  $book   = addslashes($_POST&#1111;'book']);

  $_SESSION&#1111;'author'] = $author;
  $_SESSION&#1111;'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&#1111;'author']; ?>
    </span>
    <br><br>
    Your favorite book is:
    <span style="font-weight: bold;">
      <?php echo $_SESSION&#1111;'book']; ?>
    </span>
    <br><br>
    Your favorite actor is:
    <span style="font-weight: bold;">
      <?php echo $_POST&#1111;'actor']; ?>
    </span>
    <br><br>    Your favorite president is:
    <span style="font-weight: bold;">
      <?php echo $_POST&#1111;'president']; ?>
    </span>
    <br><br>
    <div align="center">
      <a href="./temp.html">click here</a>
    </div>
  </body>
</html>
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Re: why use sessions?

Post by volka »

lanlord wrote:doesn't using sessions create more load on the server :?:
carrying the whole data all forms long is overhead, too,isn't it? ;)

just out of curiosity: why not ask for all those entries in one form? anyway.

Code: Select all

&lt;?php
$hashes = array('Author', 'Book', 'Actor', 'President'); // mandatory fields
$missing = 0;
$current = 1;
foreach($hashes as $hash)
{
	if (isset($_POST&#1111;$hash]))
		$_SESSION&#1111;$hash]=$_POST&#1111;$hash];
	else if(!isset($_SESSION&#1111;$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 --&gt; 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

?&gt;
This is just an example. There are many ways to do it.....
Post Reply