How can I split a PHP form into two sections, before submission to a database?
Hi guys,
I have set up a PHP form for a competition, for users to enter and all information to be stored in a database. I used the following NetTut+ tutorial to do so: http://tr.im/SwAd.
I've got the form submitting to the database as required, but with so many additional questions being asked, I would like to split the form into two separate sections. Obviously the first page would say continue to the next step before the second step allowing for the form to be submitted to the database.
The content that the user sees should be split, but should all be a part of the same form. Step 1 > Step 2 before submission.
Would anyone know of or recommend any methods to do this?
The content that the user sees should be split, but should all be a part of the same form. Step 1 > Step 2 before submission.
You have form entry separated over two served documents, and you want to know how to store Step 1 input until Step 2 input is obtained?
There are several approaches:
1. You can collect Step 1 input on the server side and store it in a temporary file with an identifier as to which user (a user id or session id) which you send out with Step 2 form and then receive on its submission, marrying them. PHP will store data on the filesystem itself, or you can have a temporary record created by PHP on your (MySQL?) database, which is then marked as permanent when all input is obtained and its format confirmed.
2. You can pass all Step 1 input out to the client and tucked within the Step 2 form as one or more HTML 'input' elements of type 'hidden' with 'name' and 'value' attributes set (of course contained within a 'form' element). Better is to put it all in a single '<input type="hidden" name="Step1" value=".....">' where value is perhaps a long string of a multiple set of name=value pairs delimited by a character of your choice and representing all Step 1 input.
With the code I've used, would you be able to direct me with how I could perform this second approach. I'm really clueless, as I said this is all very new to me - baby steps are unfortunately required. Thanks for your help in advance.
I supposed all the elements in the <form> container represent Step 1?
Let me set up a short example. You will have three web documents, the first being Step 1, and the second being Step 2, and the third being a Form input result document (call it Step 3). Step 1 can be an HTML or PHP document, but Steps 2 and 3 will be PHP documents, since they are processed through PHP. Now here is the content:
=== Step1.html begins ====
Code:
<!DOCTYPE ---> Strict HTML always
<html>
.
<form method="get" action="Step2.php">
<p style="margin-top:0;">
First name: <input size="10" name="firstname"><br>
Last name: <input size="10" name="lastname">
<button onclick="this.form.submit();">Submit</button>
<button onlclick="this.form.reset();">Clear all</button>
</form>
.
</html>
=== Step1.html ends ====
=== Step2.php begins ====
Code:
<!DOCTYPE ---> Strict HTML always
<html>
.
<form method="get" action="Step3.php">
<p style="margin-top:0;">
Age: <input size="3" name="age"><br>
Sex: <select name="sex">
<option>male</option>
<option>female</option>
<option>hermaphrodite</option></select>
<?php
// put each name=value pair from retrieved GET method into its own
// hidden input element
foreach ($_GET as $getkey => $getvalue)
echo "<input type=\"hidden\" name=\"$getkey\" value=\"$getvalue\">\n";
?>
<button onclick="this.form.submit();">Submit</button>
<button onlclick="this.form.reset();">Clear all</button>
</form>
.
</html>
=== Step2.php ends ====
=== Step3.php begins ====
Code:
<!DOCTYPE ---> Strict HTML always
<html>
.
<?php
// code to get all the input using $_GET array
// code to validate data
/* database insert/update code: mysql_connect(),
mysql_select_db(), mysql_query, etc., entry result checking */
if ($recordEntry == true)
echo "The data was successfully entered into the database records";
else if ($recordValidation == false)
echo "The following data entry/entries could not be validated: ".//put name=value problems here;
else if ($recordInsertion == false)
echo "There was a problem trying to enter the inputs into the database: ".mysql_error();
?>
.
</html>
=== Step3.php ends ====
The part in blue in serving Step2.php is basically all the input from Step1.html form data, put into several hidden input elements. This will again be passed when Step2.php form inputs are submitted. You only need to find a place to put the Step1.html form data. In fact, I could have made really complicated--relatively speaking--by combining all name=value pairs from Step1.html data into a single string, say
Thus all the values from first form are put into a string, and each name=value pair is delimited within by two semicolons (you can choose a delimiter). For larger amounts of data, look into various forms of serialization.
It can be in hidden input elements in the HTML document, cookies, in the server as part of its memory ($_SESSION array elements?), or in a temp file on the server filesystem.
In Step3.php, PHP checks to make sure all data is valid and that it could be recorded into database. If neither of those is true, then you create the proper response. If everything was a success, you tell the user about that too.
Bookmarks