Jump to content

Dynamically creating a form


cavedave

Recommended Posts

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

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

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.