Help with Forms

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
mhm
Forum Newbie
Posts: 2
Joined: Sat Mar 27, 2004 8:24 am

Help with Forms

Post by mhm »

Hi all,

I have a little problems with storing values from forms. This is what I am trying to do.

I have quite a large form and am trying to split into different sections (pages.)

So lets says I have a variable called section that track the form processing and I have a form structure that is the shell for the form. The I choose which for to display according to the value in the section variable.


as below

Code: Select all

$stage =1;

if (isset ($_POSTї'stage']))
{
   $stage = $_POSTї'stage'];
}


// Logon details
$form_1 = "	<tr>
			<th colspan='2'>Logon Details.</th>
			</tr>

			<tr>
			<td>Email address:</td>
			<td><input type='text' name='email_1' value=".$email_1."></td>
			</tr>

			<tr>
			<td>Re-type Email:</td>
			<td><input type='text' name='email_2' value=".$email_2."></td>
			</tr>

			<tr>
			<td>Password: </td>
			<td><input type='password' name='pass_1'></td>
			</tr>

			<tr>
			<td>Re-type Password: </td>
			<td><input type='password' name='pass_2'></td>
			<tr>";



// Contact details and personal details.
$form_2 = "<tr>
	<td width='32%'>First Name</td>
	<td><input type='text' name='fname' value=".$fname."></td>
	</tr>
	<tr>
	<td>Last Name</td>
	<td><input type='text' name='lname' value=".$lname."></td>
	</tr>";


$form_3 = "";

$form_4 =	"";

$form_5 = "<tr>
	<th colspan='2'>Conatact Details.</th>
	</tr>
	<tr>
	<td>Full Name:</td>
	<td>".$fname." ".$lname."</td>
	</tr>";

/* POST&#1111;'stage'] variable : control the registration process
* 1: Beginning stage : Login details
* 2: Contact details
* 3: Personal details
* 4: Ideal Partner
*/

$form_struct = "<table width='70%' border='0' align='center'>
	        <tr>
	        <td>
	       <form action=".$PHP_SELF." method='post' name='mem_reg'>
	<table width='100%' border='0'>
			";

$nav_forward = "<input type='submit' name='NF' value='Next'>
	<input type='hidden' name='stage' value='".++$stage."'>";
$nav_back = "<input type='button' name='NB' value='Back' onclick='history.go(-1)'>";


if ($stage == 1)
	$form_struct .= $form_1;

else if ($stage == 2)
	$form_struct .= $form_2;

else if ($stage == 3)
	$form_struct .= $form_3;

else if ($stage == 4)
	$form_struct .= $form_4;

else if ($stage == 5)
	$form_struct .= $form_5;


$form_struct .= "
		<tr>
			<td>&nbsp;</td>
			<td>&nbsp;</td>
		</tr>

		<tr>
			<td>";
$form_struct .= "	</td>
		<td>";

$form_struct .=$nav_forward;
$form_struct .= "</td></tr>";
$form_struct .= "
		</table>

		</td>
		</tr>
		</table>";



$body =  "<td valign=top width=100%>
	 ".$form_struct."
	  </form>
	  </td>";
I have removed most of the form to keep is small and simple.

Coming to the problem now.
The that I am having is that, although I try to store the values entered onto the variables. I can only hold them for next form I enter to then I loose it.

So if for example I enter my Name in form_2 I can view it on form_4 but as soon as I get to form_4 the value stored on the $fname and $lname variables are lost.

I just can't get my head round it as to why it does that.
I even tried putting conditions on the top of the page. That for example:

Code: Select all

if (isset($_POST&#1111;'fname']))
&#123;
     $fname = $_POST&#1111;'fname'];
&#125;
so that it only changes the values in the variable if it has something to store. But it didn't help.

I think i will stop writing cause I think I get confusing (to you guys and myself :) )

Any help would be highly appreciated.

Thx
Goowe
Forum Commoner
Posts: 94
Joined: Mon Mar 15, 2004 9:51 am
Location: Southeast Alaska

Post by Goowe »

If I read right, your setting up the form onto multiple pages and once the person goes past the next page they lose the $_POST array from the page before?

I think the best thing to do would be to set sessions for it and remove the sessions which the forum has been submitted error-free...

Code: Select all

<?php
// At the top of the page
session_start();

// At the end of each form
foreach($_POST as $k => $v)
{
   session_register($k);
   $_SESSION[$k] = $v;
}

// After form submission
session_unset();
session_destroy();
?>
I think as long as you don't have the same-name inputs on different pages, this should work? Good luck.

http://us4.php.net/manual/en/ref.session.php
mhm
Forum Newbie
Posts: 2
Joined: Sat Mar 27, 2004 8:24 am

Post by mhm »

Thanks Goowe.

I'll try that and let you know how it goes.

Thx once again.
Post Reply