Page 1 of 1

Generating forms within a loop

Posted: Tue Jun 27, 2006 2:10 am
by davidjwest
I wonder if someone can help me as I'm struggling with this.

I want to display two similar forms using a function within a loop, they look fine but when I test whichever submit button I click (there is one per form) it always assumes that the last submit button to be created is the one which has been pressed.

Is it possible to use variables for submit buttons and then when checking for isset($_POST) etc?

Code: Select all

<?php
echo "<input type='submit' name='stints$loopz' value='Go'>\n";

..........

if (isset($_POST['stints$loopz']))
?>
Hopefully this makes sense.

Thanks for any advice, I can post the code I've got so far if needed but it's quite long-winded.

Posted: Tue Jun 27, 2006 2:54 am
by RobertGonzalez
Have you tried it yet?

Just a little hint: Yes, you can do it.

Posted: Tue Jun 27, 2006 3:04 am
by davidjwest
Yes, I did try it and it doesn't appear to do anything when the submit button is clicked, do you think the code looks OK? If so then I'll try it again, there maybe a problem somewhere else.

Posted: Tue Jun 27, 2006 9:55 am
by RobertGonzalez
Show some more code on how you generate the forms. The names of your submit buttons are going to be kinda important, as are the id's of your forms. But show the form generating code. Also, if you can, post the HTML that is generated by the code so we can look at the forms after they have been built.

Posted: Tue Jun 27, 2006 3:27 pm
by davidjwest
Thanks, but I found the problem, it was more stupid than you could imagine!

I wasn't closing the form using the </form> tag between each iteration of the loop and so it was treating the separate forms as one, because they were one!

Doh! Sorry to waste your time, if anyone wants to see the working code let me know, I mocked up a demo version to show how it worked and then realised my mistake just as I was about to post it.

Beginners mistake..........

Thanks anyway!

:D

Posted: Tue Jun 27, 2006 3:34 pm
by RobertGonzalez
We all make 'em. Glad you were able to discover yours before one of us did :twisted: .