Multi-page forms

I have a four page registration application, on each page there are different forms, some have drop-downs which are populated from mysql, my question is what kind of options can i use to save all selections through out all the pages and insert it in the database when the user reaches the last pages and clicks finish?
ostroxAsked:
Who is Participating?
 
AielloJConnect With a Mentor Commented:
ostrox:

Let's go through your postings and get this fixed.  Starting with the first one.

<?php
require_once('Account.php');
$objUser_Account = new User_Account();
  if ('submit')
  {
    $objUser_Account -> Capture_Form_Data();
  }
?>


The file in the require_once function

  require_once('Account.php');

should be the one containing the class module code in my earlier post.  I name my class files xxx.class.php so they stand out from normal php pages.  This can cause a lot of confusion if you don't.  I also strongly suggest adding the relative or absolute path in all file references.  If the Account.php is in the folder where it's being loade from then I suggest a '.\' before the filename.

You also have an error in the line

  if ('submit')

This line is to test whether the user clicked the button or link that we want to trigger our code.  Since the code is at the top of the module, we have to have a way of controlling when it gets triggerred.  The string constant 'submit' will probably cause the code never to trigger.  Since your button is named 'submit' you have to add the POST vars to the test.  I included a function I use for this testing in the code snippet below.  Your line that reads:

  if ('submit')

should look like the following:

  if (Form_Button_Active('submit'))

My code snippet is an old library file.  You may have to change the $HTTP_POST_VARS to $_POST for your system.

As for the following section, it is where you can set variables that will persist from page to page.  This array is the one the function Capture_Form_Data appends the form contents to.  You can also define your own variables in this section.

  $this -> arrClassData =
   array
   (
    "ID"                      => 0,
    "Active"                  => "N",
 
// Add your variables here.
    "Mode"        => "Unk"
   );

You shouldn't need the function getSessionStringVariable() so I didn't include it.  The code samples provided are part of my general web library that is only provided to clients in 'compiled' form as I consider it a big part of a competative advantage, so I don't want to give more than is necessary.  This code and library do use sessions exclusively.

/**********************************************************************
' Check the status of the form button name passed.  Return true if it
' is clicked (Active), false if not.
'
' function
' Form_Button_Active
' (
'  $Btn_Name
' )
'*********************************************************************/
 
function
Form_Button_Active($Btn_Name)
 
{
  global $HTTP_POST_VARS;
 
  $blnBtn_Active = false;
  
/**********************************************************************
'
' Test the status of the button name passed.
'
'*********************************************************************/
 
  if (isset($HTTP_POST_VARS[$Btn_Name]))
  {
    if ($HTTP_POST_VARS[$Btn_Name] <> ""  &&
        $HTTP_POST_VARS[$Btn_Name] <> "N")
    {
      $blnBtn_Active = true;
    }
  }
 
  return($blnBtn_Active);
}

Open in new window

0
 
hieloCommented:
you can pass them from page to page via hidden fields or via session variables.
0
 
Loganathan NatarajanLAMP DeveloperCommented:
better you save into the db in each page process completed .. then take the id as the reference for the other pages and complete the form...
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
MMDeveloperCommented:
or use css/javascript to create the multi-page form and the last page does the one and only submit.
0
 
AielloJCommented:
You might want to check out my answer to a prior post like this.  The best way to do this is to capture your form variables to an associative array when each page is submitted.  You then have an array containing all of each pages form elements accessable by name.  ie:

Page 1: FirstName, LastName
Page 2: Address
Page 3: City, State, Zip

If captured into an associative array named FormData you have:

FormData['FirstName']
FormData['LastName']
FormData['Address']
.. etc

You can then write the record to the database in one operation and it's cleaner than hidden form variables.  The code snippet attached to the link below has my class module I use in all my PHP/database projects.

http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/Q_23728797.html
0
 
ostroxAuthor Commented:
Thanks for the replies, aielloJ i'll give it a try tomorrow with your example seems alright so far, i was wondering with this system, how would i be able to remeber a value in a drop down form (from filled from mysql), so that next time the user wants to select that same value, it would not be avaliable to them, since they might already choosen that same value in a previous application, instead it should appear there but be disabled to them for selecting?
0
 
AielloJCommented:
ostrox:

The easiest way to make sure the user doesn't pick a value they used in a previous application is to eliminate it from the drop down box.  You can't disable certain elements of a combo box.  If they're visible they're selectable.  You can do it with Javascript from your form.  See the link:

http://www.java2s.com/Code/JavaScript/Form-Control/Removeanoptionfromadropdownlistcombobox.htm
0
 
ostroxAuthor Commented:
Hi Aielloj,

The solution you posted in the link you posted earlier, how do i use that? where do i put it?  I'm creating a fina.php file which has all my insert statement in it, but how do i make sure all values from each page, four in total, reach to that final.php file... i looked at the code in the first post, that's to complicated as i need to have this project done, today! hope you can reply soon!

what do i need to have on all pages? session_start(); ?
0
 
AielloJCommented:
ostrox:

I've included a file below named Pageheader.txt that shows what is needed at the top of your php files.  If the user clicked whatever button you have for submitting the form, it instantiates the class object and calls the function that saves all of the data elements on the form into the associative array.  It does this by looping through all of the elements that were in that pages form and inserting elements with the same name into the associative array.
PageHeader.txt
Account-AE.class.txt
0
 
ostroxAuthor Commented:
Hi Aielloj,

Just to see if i got it, i place the PageHeader code in all my php files and include the Account-AE.class file with my insert.php file or how would it work... ps, I have text links name previous and next on each form page, what code would i need to add so this works with it?
0
 
AielloJCommented:
ostrox:

I think you've got it.  The code in the PageHeader file goes at the top of each web page that is part of the multiple page input process.  You also need to put a 'require_once' directive with the filename of the class file.  You can change the name of the class file, and the name of the class it creates inside the file to whatever matches your application.

By creating different .class.php files from the one I sent, you can use this scheme for almost any page.  Just make sure to change the name of the array in the line below to something unique.

  $this -> strSession_Var_Name = "svrUser_Account_Form";

You need to add a function to the class module to retrieve named array elements.  You can use the code in the attached snippet.

You need to add a call to capture the form's data using the function whenever your previous or next link is clicked.  I usually use a form button which causes a POST operation to get the form contents into the POST variables array.  You might want to use some Javascript to force the form to be submitted when a user clicks the previous or next links.  I think if a link is inside a form element it will do a POST and a GET submission.  To get an idea what this class does, I suggest creating one or two simple forms with buttons to Submit the form data.  Using part of my prior reply create forms with the following input text boxes and place the Pageheader code at the top of the page the form's Action property points to.

Page 1: FirstName, LastName
Page 2: Address
Page 3: City, State, Zip

You can then write test code to see the values captured by:

  echo $objUser_Account -> getClassMemberValue('FirstName');
  echo $objUser_Account -> getClassMemberValue('LastName');


/**********************************************************************
' Get method to get the variable named in the $MemberName parameter.
' This member function allows us to have a single 'getXXX' function
' for all of the class member variables.
'
' function
' getClassMemberValue
' (
'  $MemberName
' )
'*********************************************************************/
 
function
getClassMemberValue($MemberName)
{
  if (isset($MemberName))
  {
    $MemberValue = $this -> arrClassData[$MemberName];
  }
  else
  {
    $FatalError  = TRUE;
    $ErrorString = "Unknown variable name: " .
                    $MemberName .
                   " in class 'Client'";
 
    $MemberValue = "";
  }
 
  return $MemberValue;
}

Open in new window

0
 
ostroxAuthor Commented:
Hey thanks for that last post, but i've come to a dead end, i can't get this to work, i did a simple three page with forms on each page thing as you mentined but nothing worked,

here what i have so far: a file called accounts.php which has the whole Account-AE.class.txt script in it, that's included on the first line of all of the three pages, and right under that on all three pages i added the pageheader code, here what page1.php looks like:




<?php 
require_once('Account.php');
$objUser_Account = new User_Account();
  if ('submit')
  {
    $objUser_Account -> Capture_Form_Data();
  }
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>
 
<body>
<form id="form1" name="form1" method="post" action="page2.php">
  <label>Name:
  <input type="text" name="FirstName" id="FirstName" />
  </label>
  <br />
  <label>Age
  <input type="text" name="LastName" id="LastName" />
  </label>
  <label><br />
  Submit
  <input type="submit" name="submit" id="submit" value="Submit" />
  </label>
</form>
</body>
</html>

Open in new window

0
 
ostroxAuthor Commented:
and page4.php where i wanted all my values to be displayed looks like this:


<?php 
require_once('Account.php');
function
getClassMemberValue($MemberName)
{
  if (isset($MemberName))
  {
    $MemberValue = $this -> arrClassData[$MemberName];
  }
  else
  {
    $FatalError  = TRUE;
    $ErrorString = "Unknown variable name: " .
                    $MemberName .
                   " in class 'Client'";
 
    $MemberValue = "";
  }
 
  return $MemberValue;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>
 
<body>
<?php
 
  echo $objUser_Account -> getClassMemberValue('FirstName');
  echo $objUser_Account -> getClassMemberValue('LastName');
?>
</body>
</html>

Open in new window

0
 
ostroxAuthor Commented:
Just as a note, for the real application, i have a final page, so that when the user passes through all four pages and enters or selects values, the last page, has the full sql insert, would that work with your example, the concept seems interesting, i hope i can get a reply from you soon and implement this, thanks again for your assistance so far.
0
 
hieloCommented:
@AielloJ: I was looking over your class file, and I am wondering if you attached the correct file. I am noticed you have:

...
  else
  {
    $this -> arrClassData =
      $this -> getSessionStringVariable($this -> strSession_Var_Name);
  }
...
 
but I don't see the getSessionStringVariable() implemented anywhere!
 
Also, what is the significance of this in Initialize():
 
  $this -> arrClassData =
   array
   (
    "ID"                      => 0,
    "Active"                  => "N",
 
// Add your variables here.
    "Mode"        => "Unk"
   );
 
perhaps I am missing something, but I don't understand what those variables are supposed to be.

Open in new window

0
 
ostroxAuthor Commented:
Could someone please just show me how i can do this with sessions? i need this done by today, really appreciate a quick reply because the above code is taking too long to figure out and implement.  Unless i can get a working example to look at?
0
 
ostroxAuthor Commented:
Can someone help me with this code, i can't get it to work properly so that i goes to the next page?
<?php 
include("connection.php");
// each page needs a different name for the submit button
if (array_key_exists('Submit1', $_POST)) {
  session_start();
  include('corefuncs.php');
  nukeMagicQuotes();
  // clear any existing session variables
  $_SESSION = array();
  // set a variable to control access to other pages
  $_SESSION['formStarted'] = true;
  // set required fields
  // must be an array, even if only one item is required
  // if no fields are required, an empty array is needed
  // otherwise, in_array() later in the script will generate an error
  $required = array('bid');
  $required = array('mid');
  // create empty array for any missing fields
  $missing = array();
  
  // process the $_POST variables and save them in the $_SESSION array
  foreach ($_POST as $key => $value) {
    // assign to temporary variable and strip whitespace if not an array
    $temp = is_array($value) ? $value : trim($value);
	// if empty and required, add to $missing array
	if (empty($temp) && in_array($key, $required)) {
	  array_push($missing, $key);
	  }
	// otherwise, assign to a variable of the same name as $key
	else {
	  $_SESSION[$key] = $temp;
	  }
	}
  // if no required fields are missing, redirect to next page
  if (!$missing) {
    header('Location: perinfo.php');
	exit;
	}
  }
?>
 
 
<?php 
$query_bach = "SELECT * FROM bachelor ORDER BY bid";
$query_type = mysql_query($query_bach);
$query_rows = mysql_fetch_assoc($query_type);
?>
 
 
<?php 
$query_mast = "SELECT * FROM master ORDER BY mid";
$query_mtype = mysql_query($query_mast);
$query_mrows = mysql_fetch_assoc($query_mtype);
?>
 
<html>
<head>
<title>test01</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="main.css" rel="stylesheet" type="text/css">
  <script type="text/javascript" src="livevalidation_standalone.compressed.js"></script>
</head>
 
 
 
 
<body>
<div id="container">
<div id="header">
<p class="nextprev"><?
 
echo "Welcome: " .$_SESSION['email']. " | <a href='logout.php'>Logout</a>";
?></p>
</div>
<div id="nav">
<p>Online applications </p>
</div>
<div class="content">
 
<h2>Apply online</h2>
 
</div>
<div class="clear"></div>
<div class="content">
<div id="form_login">
<fieldset>
<h4>Select Programme</h4>
<p>Bachelor Programmes: </p>
<?php
if (isset($missing)) {
  echo "<p>$missing[0] missing</p>";
  }
?>
<form action="perinfo.php" method="post">
<select name="bid" id="email_field">
 
<?php 
do {
 
  echo "<option value=\"".$query_rows['bid']."\">".$query_rows['title']."</option>";
}
while ($query_rows = mysql_fetch_assoc($query_type));
?>
 
</select>
<input type="submit" class="submit" name="Submit1" value="Start Application">
</form>
</fieldset>
<hr size="1">
<fieldset>
<h4>Select Programme</h4>
<p>Master Programmes: </p>
<form action="perinfo.php" method="post">
<select name="mid" id="email_field">
 
<?php 
do {
 
  echo "<option value=\"".$query_mrows['mid']."\">".$query_mrows['title']."</option>";
}
while ($query_mrows = mysql_fetch_assoc($query_mtype));
?>
 
</select>
<input type="submit" class="submit" name="Submit1" value="Start Application">
</form>
</fieldset>
</div>
<div class="clear"></div>
 
<p id="title">Existing applications </p>
<div class="clear"></div>
 
<div id="form_login">
<fieldset>
 
  <br>
  View</p>
</fieldset>
</div>
 
</div>
<div id="footer">&copy; 2008 </div>
</div>
 
</body>
</html>

Open in new window

0
 
ostroxAuthor Commented:
Hey Aielloj, could you please see this post... i tried something different in the mean while becuase i need to have this done in 2 hours (hopefully)

http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/Q_23924914.html

There are attached files for you to see.
0
 
AielloJCommented:
Responded as requested.
0
 
ostroxAuthor Commented:
Thanks for the info aielloj, would it be possible for me to send you my files so you can give them a check and see if there is something missing?
0
 
AielloJCommented:
Yes.  I'm willing to look them over.  Once you get them working the same a lot of time.  The best thing about them is if you add a field to your form, you don't have to modify the code at all because the loop in the code looks at all the fields on your form and save them.

The only thing you have to be aware of is:
1) For the code to work, you cannot have the same form field name on other pages.  (ie: FName on form 1, and also on form2)
2) Items like the submit button names from the form are also saved, but just ignore them.

Thanks for the points.  I'll look for your post on the syntax if you haven't gotten an answer.

AielloJ
0
All Courses

From novice to tech pro — start learning today.