Two forms on the same page

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?
LVL 1
winmeisterAsked:
Who is Participating?
 
Diablo84Connect With a Mentor Commented:
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
 
RoonaanCommented:
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
 
Diablo84Commented:
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
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
winmeisterAuthor Commented:
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
 
Diablo84Commented:
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
 
Diablo84Commented:
Make sure you have

session_start();

at the top of the page otherwise you wont be able to initialize the session data.
0
 
winmeisterAuthor Commented:
Does it work also for "select" fields other than "input"?
0
 
Diablo84Commented:
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
 
Diablo84Commented:
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
 
Diablo84Commented:
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
 
winmeisterAuthor Commented:
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
 
Diablo84Commented:
As long as each form has different names for the components it should maintain the values, can you post your code please
0
 
winmeisterAuthor Commented:
<?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
 
winmeisterAuthor Commented:
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
 
Diablo84Commented:
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
 
winmeisterAuthor Commented:
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
 
Diablo84Commented:
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
 
winmeisterAuthor Commented:
Thank you so much...
0
 
Diablo84Commented:
no problem :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.