开发者

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

开发者 https://www.devze.com 2022-12-24 01:00 出处:网络
I have set up a PHP form for a competition for users to enter all information to be stored in a database. I used a NetTut+ tutorial to do so.

I have set up a PHP form for a competition for users to enter all information to be stored in a database. I used a NetTut+ tutorial to do so.

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've attached the code below.

<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=开发者_Go百科"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> 


Use sessions mechanism to store 1 step data


You could pass them to page two and then put them in as hidden variables. You could also use session variables.

example with hidden fields


Sessions are usually the preferred way to do this, but hidden form fields would work just as well.

It's pretty easy to do - after the first submission, store the values into the session - validate them first if you like, it's probably a good idea to do so in fact. Then go to the next page, and once submitted, validate the second bunch of answers and put them into the database.

Hidden form fields work too, but I prefer the session-based approach.

Good luck!


One other option would be to put the additional fields in a hidden div on the same page and use javascript to show them once the first set have been completed. The advantage for you is that it keeps your form processing simpler. Also for your users, they won't have a round trip to the server to get the other part of the form.

This, of course, requires that your users have javascript turned on. The best practice would be to show all fields on the same page by default, and then use JS to hide the second batch before the page renders.

0

精彩评论

暂无评论...
验证码 换一张
取 消