cavedave Posted August 12, 2009 Share Posted August 12, 2009 Hello I have a form that takes in a number of people and a number of items. For each person and each item I then want the user to be able to input a value. So if they decide the want 3 people and 4 items. I want a form that takes in 12 different values. Person1 Person2 Person3 Item1 Item2 Item3 Item4 Is there a way to dynamically create a form with the right number of inputs in php? I can get the number of people and items inputed it is just given those to create a form witht hat number of inputs. I have tried using a foreach loop to create a form for each person <?php for ( $counter = 1; $counter <= $people; $counter += 1) { echo "Valuation item"; echo $counter; echo ": <input name="; echo "\$items["; echo $counter; echo "];"; echo "type=\"text\" />" ; } ?> <form action="process.php" method="post"> <input type="submit" /> </form> Apologies if this question is idiotic. Thank you for any advice you can give me. Link to comment https://forums.phpfreaks.com/topic/169920-dynamically-creating-a-form/ Share on other sites More sharing options...
otuatail Posted August 12, 2009 Share Posted August 12, 2009 You could ask the user how many they wanted first. Then take them to a second form that would have input boxes for each item. You would also need the form reading the results to also know how many. Also you would need a naming convention. Something along these lines should do <?php for($x=0;$x<$Total;$x++) { // $input = "MyInput_" . $x; // MyInput_0 MyInput_1 MyInput_2 etc ?> <input type="text" name="<? $input?>" value=""> <?}?> each input box would have it's own unique name. Desmond Link to comment https://forums.phpfreaks.com/topic/169920-dynamically-creating-a-form/#findComment-896391 Share on other sites More sharing options...
trq Posted August 12, 2009 Share Posted August 12, 2009 This would be much better done using javascript rather than php. Link to comment https://forums.phpfreaks.com/topic/169920-dynamically-creating-a-form/#findComment-896395 Share on other sites More sharing options...
cavedave Posted August 12, 2009 Author Share Posted August 12, 2009 Thank you thorpe and otuatail. I will try it in javascript and post back how i get on. Link to comment https://forums.phpfreaks.com/topic/169920-dynamically-creating-a-form/#findComment-896491 Share on other sites More sharing options...
cavedave Posted August 13, 2009 Author Share Posted August 13, 2009 There is a good article here on how to add dynamic forms using javascript Link to comment https://forums.phpfreaks.com/topic/169920-dynamically-creating-a-form/#findComment-897673 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.