www.webdeveloper.com
Results 1 to 4 of 4

Thread: How can I split a PHP form into two sections, before submission to a database?

Hybrid View

  1. #1
    Join Date
    Jun 2009
    Posts
    12

    Unhappy 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?

    I'm a beginner so please be nice.

    Cheers,

    Daniel

  2. #2
    Join Date
    Jun 2009
    Location
    Miami
    Posts
    118
    Quote Originally Posted by banjax View Post
    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.

  3. #3
    Join Date
    Jun 2009
    Posts
    12
    Hi Mavigozler,

    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.

    Code:
    <form method="post" action="">
    
        <fieldset>
        
        	<ul>
        		<li>
        			<label for="code">Entry Code On-Pack</label>
        			<input type="text" name="code" />
                </li>
                <li>
        			<label for="name">Name</label>
        			<input type="text" name="name" />
        		</li>
        		<li>
        			<label for="email">Email</label>
        			<input type="text" name="email" />	
        		</li>
                <li>
        			<label for="addressone">Address</label>
        			<input type="text" name="addressone" />
                </li>
                <li>
                	<label for="addressone">&nbsp;</label>
                    <input type="text" name="addresstwo" />
                </li>
                <li>
                	<label for="addressone">&nbsp;</label>
                    <input type="text" name="addressthree" />		
        		</li>
        		<li>
        			<label for="telephone">Telephone</label>
        			<input type="text" name="telephone" />	
        		</li>
        		<li>
        			<label for="dob">Date of Birth</label>
        			<input name="dob" type="text" value="[dd/mm/yy]" />	
        		</li>
                <li>
                	<label for="q1">Where have you seen Cookstown advertised?</label><br />
                    <input type="checkbox" name="q1cb1" /><label for="q1cb1">Magazines</label><br />
                    <input type="checkbox" name="q1cb2" /><label for="q1cb2">Billboards</label><br />
                    <input type="checkbox" name="q1cb3" /><label for="q1cb3">Television</label><br />
                    <input type="checkbox" name="q1cb4" /><label for="q1cb4">Radio</label><br />
                    <input type="checkbox" name="q1cb5" /><label for="q1cb5">Online</label><br />
                    <input type="checkbox" name="q1cb6" /><label for="q1cb6">Public Transport</label><br />
                    <input type="checkbox" name="q1cb7" /><label for="q1cb7">Bus Stops</label><br />
                </li>
                <li>
                	<label for="q2">How well do you remember those advertisments?</label><br />
                    <input type="radio" name="q2" value="VeryWell"/><label for="q1cb1">Very well</label><br />
                    <input type="radio" name="q2" value="FairlyWell"/><label for="q1cb2">Fairly well</label><br />
                    <input type="radio" name="q2" value="FewDetails"/><label for="q1cb3">A few details</label><br />
                    <input type="radio" name="q2" value="NotAtAll"/><label for="q1cb4">Not at all</label><br />
                </li>
                	<label for="tc">Do you accept the terms and conditions</label>
                    <input type="checkbox" name="tc" class="styled" />
                </li>
                <li>&nbsp;</li>
        		<li>
        			<input type="submit" value="Enter Competition" class="large blue button" name="signup" />			
        		</li>
        	</ul>
        	
        </fieldset>
        
    </form>

  4. #4
    Join Date
    Jun 2009
    Location
    Miami
    Posts
    118
    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

    <input type="hidden" name="getvalues" value="name1=value1;;name2=value2;;name3=value3">

    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.


    Look at these pages:


    http://www.php.net/manual/en/reserved.variables.get.php
    http://tr.php.net/manual/en/control-...es.foreach.php
    http://us3.php.net/manual/en/book.mysql.php

    HTH

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles