Re: Dynamic Forms
| From: | Justin French | Date: | Thu, 15 Jan 2004 16:37:24 +0000 |
| Subject: | Re: Dynamic Forms | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-174677@lists.php.net to get a copy of this message | ||
On Friday, January 16, 2004, at 02:15 AM, Robert Temple wrote:
Can anyone suggest a PHP solution to creating a form that gets built after a client enters a variable? For example: an automobile insurance form that first asks how many cars you have, and then creates a form with fields for car 1, car 2, etc. I have found JavaScript code that will do this, but I would rather use a server side language like PHP for more consistency and browser compatibility.step1.php --- <form action='step2.php' method='post'> How many cars do you own? <input type='text' size='2' name='numberOfCars'> <input type='submit'> </form> --- step2.php --- <form action='step3.php' method='post'> <? $i=0; while($i < $_POST['numberOfCars']) { $i++; echo "<h2>car number {$i}</h2>"; echo "Make <input type='text' size='50' name='car[{$i}][\"make\"]'><br>"; echo "Model <input type='text' size='50' name='car[{$i}][\"model\"]'><br>"; echo "<hr>"; } ?> <input type='submit' /> </form> --- This will produce an array of cars ($car[1] to $car[n]), each of which is an array of two elements make and model. Haven't done any work with arrays? Now is a good time :) step3.php --- <pre><? print_r($_POST) ?></pre> --- This code is there just to show you the array structure... you'd change it to ad items to a database, etc. Example output: --- Array (
[car] => Array
(
[1] => Array
(
["make"] => datsun
["model"] => 1600
)
[2] => Array
(
["make"] => toyota
["model"] => corolla
)
[3] => Array
(
["make"] => honda
["model"] => civic
)
)) --- Good luck :) Justin