Generating forms within a loop

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
davidjwest
Forum Commoner
Posts: 67
Joined: Sat Nov 06, 2004 5:26 am
Location: Leeds, Yorkshire, England

Generating forms within a loop

Post 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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Have you tried it yet?

Just a little hint: Yes, you can do it.
davidjwest
Forum Commoner
Posts: 67
Joined: Sat Nov 06, 2004 5:26 am
Location: Leeds, Yorkshire, England

Post 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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
davidjwest
Forum Commoner
Posts: 67
Joined: Sat Nov 06, 2004 5:26 am
Location: Leeds, Yorkshire, England

Post 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
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

We all make 'em. Glad you were able to discover yours before one of us did :twisted: .
Post Reply