Perad Posted October 25, 2006 Share Posted October 25, 2006 I have come up with a loop which generates a form. I am pretty stunned that it worked first time 8).Anyway, i need to transfer data from this form into a database. Because the form size is constantly varying i don't know where i would start. Would i need to use a loop to generate a mysql command?[code]Heres my loop to generate the form. if ($players !== 0) { echo '<form id="form2" name="form2" method="post" action="">'; echo 'UNC<br /><br />'; for ($i=0;$i<$players;$i++) { if (($players/2)==$i) { echo '<br /><br />Opponent<br />Player:<input type="text" name="player'.$i.'" size="35" maxlength="35" /><br />'; } else { echo 'Player:<input type="text" name="player'.$i.'" size="35" maxlength="35" /><br />'; } } echo '</form>'; } [/code]Any ideas on how i would handle this form? Quote Link to comment Share on other sites More sharing options...
effigy Posted October 25, 2006 Share Posted October 25, 2006 The name of the inputs should be[tt] player[][/tt], which will come across as an array named[tt] $_POST['player'][/tt]. You can then loop through the array and run your SQL statements. Quote Link to comment Share on other sites More sharing options...
Perad Posted October 25, 2006 Author Share Posted October 25, 2006 [quote author=effigy link=topic=112677.msg457413#msg457413 date=1161787209]The name of the inputs should be[tt] [/tt], which will come across as an array named[tt] $_POST['player'][/tt]. You can then loop through the array and run your SQL statements.[/quote]Could you explain this more, do you mean...player[<input type="text" name="player'.$i.'" size="35" maxlength="35" />] Quote Link to comment Share on other sites More sharing options...
effigy Posted October 25, 2006 Share Posted October 25, 2006 Change all of your inputs to [tt]<input type="text" name="player[]" size="35" maxlength="35" />[/tt] and add [tt]echo '< pre>', print_r($_POST, 1), '< /pre>';[/tt] to the submission page. Then submit and see what happens.P.S. Take the spaces out of the pre tags. Quote Link to comment Share on other sites More sharing options...
Perad Posted October 25, 2006 Author Share Posted October 25, 2006 Sorry to take so long, until you editted it your post didn't make a great deal of sense :)It comes back with [code]Array( [player] => Array ( [0] => 1 [1] => 2 ) [submit2] => Next)[/code]1 and 2 being the values which i put in. How would i incorporate this into a loop? Quote Link to comment Share on other sites More sharing options...
effigy Posted October 25, 2006 Share Posted October 25, 2006 [code]foreach ($_POST['player'] as $key => $value) { echo "Code to process $value<br/>";}[/code] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.