Results 1 to 4 of 4

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

  1. #1
    Join Date
    Jun 2009

    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.



  2. #2
    Join Date
    Jun 2009
    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
    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.

    <form method="post" action="">
        			<label for="code">Entry Code On-Pack</label>
        			<input type="text" name="code" />
        			<label for="name">Name</label>
        			<input type="text" name="name" />
        			<label for="email">Email</label>
        			<input type="text" name="email" />	
        			<label for="addressone">Address</label>
        			<input type="text" name="addressone" />
                	<label for="addressone">&nbsp;</label>
                    <input type="text" name="addresstwo" />
                	<label for="addressone">&nbsp;</label>
                    <input type="text" name="addressthree" />		
        			<label for="telephone">Telephone</label>
        			<input type="text" name="telephone" />	
        			<label for="dob">Date of Birth</label>
        			<input name="dob" type="text" value="[dd/mm/yy]" />	
                	<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 />
                	<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 />
                	<label for="tc">Do you accept the terms and conditions</label>
                    <input type="checkbox" name="tc" class="styled" />
        			<input type="submit" value="Enter Competition" class="large blue button" name="signup" />			

  4. #4
    Join Date
    Jun 2009
    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 ====
    <!DOCTYPE ---> Strict HTML always
    <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>
    === Step1.html ends ====

    === Step2.php begins ====
    <!DOCTYPE ---> Strict HTML always
    <form method="get" action="Step3.php">
    <p style="margin-top:0;">
    Age: <input size="3" name="age"><br>
    Sex:  <select name="sex">
    // 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>
    === Step2.php ends ====

    === Step3.php begins ====
    <!DOCTYPE ---> Strict HTML always
    //  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();
    === 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:



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



X vBulletin 4.2.2 Debug Information

  • Page Generation 0.11690 seconds
  • Memory Usage 2,898KB
  • Queries Executed 15 (?)
More Information
Template Usage (34):
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (4)bbcode_code
  • (1)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (4)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (4)postbit
  • (4)postbit_onlinestatus
  • (4)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (73):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_postinfo_query
  • fetch_postinfo
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates