felix_xiao
felix_xiao

Reputation: 1628

HTML More Than One Form With Same Name/ID

I'm currently designing a website that reads information from a MySQL database in PHP and prints out a form for each row. This is done using a while loop, so the form is echoed out inside the loop. However, whenever I submit the form, a lot of the values aren't posted because there are forms with the same NAME and ID on the page.

How can I print out one form per item in the database and process this form accordingly? Thanks!

while (*this goes through each row in the database) {
    echo "<form name='myForm' id='myForm' method='post' action=''>
          <input type='text' name='text1'>
          </form>";
}

I want to use a loop to print out a dynamic number of forms.

Upvotes: 0

Views: 92

Answers (1)

Mike Brant
Mike Brant

Reputation: 71384

Just change the form name and id with each iteration by adding a counter to your loop.

$i = 0;
while (*this goes through each row in the database) {
    echo "<form name='myForm" . $i . "' id='myForm" . $i . "' method='post' action=''>
          <input type='text' name='text1'>
          </form>";
    $i++;
}

Or possibly even better is if you have some sort of unique database id for each record, use that id value instead of a counter.

Upvotes: 2

Related Questions