Solved

Two forms on the same page

Posted on 2004-09-23
19
664 Views
Last Modified: 2011-10-03
I need to have two forms on the same page, both of them with action=php_self. The problem is that when submitting one form, the other form is resetted to its default values and all variables are set to empty. How can I keep variables of the two forms independent?
0
Comment
Question by:winmeister
  • 11
  • 7
19 Comments
 
LVL 49

Expert Comment

by:Roonaan
ID: 12132370
You could submit the form to a new window:
<form target="_blank">

You could use iframes and have each form contained in a seperate one.

You could build one form instead of 2 and disregard the values of the part that you do not want to be submitted.

-r-
0
 
LVL 27

Expert Comment

by:Diablo84
ID: 12132398
use sessions

session_start(); //at top of page

When a form is posted assign its values to session variables eg:

$_SESSION['variable_name'] = $_POST['input_name'];

then in each input field output its value using isset and the session variable

eg.

<input type="text" name="input" value="<?php if (isset($_SESSION['name'])) echo $_SESSION['name']; ?>">

where name corresponds the the name of the session that you have assigned that fields post data to

There are other methods of doing this such as passing the values in the query string to maintain them but each require more work then is necessary for a simple thing.
0
 
LVL 1

Author Comment

by:winmeister
ID: 12132711
Mmmh. Sure you are right, but I couldn't get it work... Could you pls supply few lines of sample code? I don't understand between all the 'name" variable that you have set... I'm a little bit confused. (variable_name, name, input_name)??
0
 
LVL 27

Expert Comment

by:Diablo84
ID: 12132770
Each input field in your form has a name that you have assigned to it, for example

<input type="text" name="first_name">
<input type="text" name="last_name">

When you post your form the values of these inputs can be accessed using $_POST['name_of_input']; for example:

echo "First name: ".$_POST['first_name'];
echo "Last name: ".$_POST['last_name'];

Now you can then assign these values to a session, the session variable can be named as anything you want, much like a normal variable. For the sake of simplicity in this case we will give the session variables names matching the input names, for example:

$_SESSION['first_name'] = $_POST['first_name'];
$_SESSION['last_name'] = $_POST['last_name'];

You can then use these variable using:

echo $_SESSION['first_name'];
echo $_SESSION['last_name'];

Now to tie this in with the original point, in the actual form, you would want to do something like this:

<input type="text" name="first_name" value="<?php if (isset($_SESSION['first_name'])) echo $_SESSION['first_name']; ?>">
<input type="text" name="last_name" value ="<?php if (isset($_SESSION['first_name'])) echo $_SESSION['last_name']; ?>">

The value is assigned like so, if the session variable has been set then output the value of the session variable in the inputs value parameter.
0
 
LVL 27

Expert Comment

by:Diablo84
ID: 12132789
Make sure you have

session_start();

at the top of the page otherwise you wont be able to initialize the session data.
0
 
LVL 1

Author Comment

by:winmeister
ID: 12132927
Does it work also for "select" fields other than "input"?
0
 
LVL 27

Expert Comment

by:Diablo84
ID: 12132949
For select fields you will need to do this:

<select name="options">
 <option<?php if (isset($_POST['options']) && $_POST['options'] == "Value1") echo "selected"; ?>>Value1</option>
 <option<?php if (isset($_POST['options']) && $_POST['options'] == "Value2") echo "selected"; ?>>Value2</option>
 <option<?php if (isset($_POST['options']) && $_POST['options'] == "Value3") echo "selected"; ?>>Value3</option>
</select>

Hopefully you will be able to see how it works from that.
0
 
LVL 27

Expert Comment

by:Diablo84
ID: 12132961
Basically the php statement for each options says if the select value is set (the form has been subitted) and the value it contains matches the options value then output selected in the tag.
0
 
LVL 27

Expert Comment

by:Diablo84
ID: 12132983
To do the same with sessions, when you first process the code:

$_SESSION['options'] = $_POST['options'];

then for the select box

<select name="options">
 <option<?php if (isset($_SESSION['options']) && $_SESSION['options'] == "Value1") echo "selected"; ?>>Value1</option>
 <option<?php if (isset($_SESSION['options']) && $_SESSION['options'] == "Value2") echo "selected"; ?>>Value2</option>
 <option<?php if (isset($_SESSION['options']) && $_SESSION['options'] == "Value3") echo "selected"; ?>>Value3</option>
</select>

If you select boxes are multi select you will need a slightly different approach, if this is the case let me know and il show you how
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 1

Author Comment

by:winmeister
ID: 12133248
OK. Your code is working fine, but for some reason it doesn't  fix my original problem. I have 2 forms on the same page and when I "submit" one form, the other is resetted, even with the session technique. Infact if I set $_SESSION['name_form1'] it works, until I submit form2. When I click on submit form 2, the $_SESSION['name_form1'] is becomes un-set and I lost previous setting.
0
 
LVL 27

Expert Comment

by:Diablo84
ID: 12133270
As long as each form has different names for the components it should maintain the values, can you post your code please
0
 
LVL 1

Author Comment

by:winmeister
ID: 12133339
<?php
session_start();
header("Cache-control: private");
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<?php
$_SESSION['guests'] = $_POST['guests'];
$_SESSION['name'] = $_POST['name'];       
?>

<html><head>
   
    </head>


<br>

<form name="prova" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">              
<select name="guests" size="1"  onChange="this.form.submit();">
<option <?php if (isset($_POST['guests']) && $_POST['guests'] == "1") echo "selected"; ?>>1</option>
<option <?php if (isset($_POST['guests']) && $_POST['guests'] == "2") echo "selected"; ?>>2</option>
</select>
</form>

<br>

<form name ="prova2" action="<?php echo $HTTP_SERVER_VARS['PHP_SELF']; ?>" method="post">
            Name:  <input type="text" name="name" value="<? if(isset($_SESSION['name'])) echo $_SESSION['name']; ?>"><br>
                        Email: <input type="text" name="personal[email]"><br>      
<input type="hidden" name="action" value="submitted">
<input type="submit" name="submit" value="submit me!">
 </form>
<br><br><hr>


.....
....
.... This section contains query code.                                                                                                                                
                                                           
</body></html>
0
 
LVL 27

Accepted Solution

by:
Diablo84 earned 100 total points
ID: 12133415
Try it like this:

<?php
session_start();
header("Cache-control: private");
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<?php
if (isset($_POST['guests'])) $_SESSION['guests'] = $_POST['guests'];
if (isset($_POST['name'])) $_SESSION['name'] = $_POST['name'];      
?>

<html><head>
   
</head>


<br>

<form name="prova" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">              
<select name="guests" size="1"  onChange="this.form.submit();">
<option <?php if (isset($_SESSION['guests']) && $_SESSION['guests'] == "1") echo "selected"; ?>>1</option>
<option <?php if (isset($_SESSION['guests']) && $_SESSION['guests'] == "2") echo "selected"; ?>>2</option>
</select>
</form>

<br>

<form name ="prova2" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Name:  <input type="text" name="name" value="<? if(isset($_SESSION['name'])) echo $_SESSION['name']; ?>"><br>
Email: <input type="text" name="personal[email]"><br>    
<input type="hidden" name="action" value="submitted">
<input type="submit" name="submit" value="submit me!">
 </form>
<br><br><hr>


.....
....
.... This section contains query code.                                                                                                                                
                                                           
</body></html>
0
 
LVL 1

Author Comment

by:winmeister
ID: 12135416
It worked now, but I had to make a last change.

<option <?php if ($_SESSION['guests']  == "1") echo "selected"; ?>>1</option>
<option <?php if ($_SESSION['guests']  == "2") echo "selected"; ?>>2</option>
0
 
LVL 27

Expert Comment

by:Diablo84
ID: 12135468
Did it not work for you with the isset condition in place?

The presence of isset wont actually make any difference to the scripts functionality it just prevents the script generating notices, if your level of error checking has been reduced they wont display anyway (as seems to be the case).

Good luck with your code.
0
 
LVL 1

Author Comment

by:winmeister
ID: 12143519
Sorry Diablo,

I need to trigger the "onchange" the first time the user opens the page, so that to set the $_SESSION variables... As one of these variables opens a database, if it is not set when the user reaches the page, no database is opened. If the user changes the select selection, it is triggered.... Do you have any suggestion?
0
 
LVL 27

Expert Comment

by:Diablo84
ID: 12144565
How about setting default values for the sessions?

for example before:

if (isset($_POST['guests'])) $_SESSION['guests'] = $_POST['guests'];
if (isset($_POST['name'])) $_SESSION['name'] = $_POST['name'];      

add:

if (!isset($_SESSION['guests'])) $_SESSION['guests'] = 1;
//if (!isset($_SESSION['name'])) $_SESSION['name'] = "a name";    

The above basically reads if the session for guests is not set set it to 1, the script should handle the rest.
Note that the second line, i have commented out because im not sure if you need it, I figure you probably don't need a default value for the name but its there in case you need to see how to do it.  
0
 
LVL 1

Author Comment

by:winmeister
ID: 12150963
Thank you so much...
0
 
LVL 27

Expert Comment

by:Diablo84
ID: 12151007
no problem :)
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now