user14576
user14576

Reputation: 43

how to listen event from form in dynamic number of forms php

I have some PHP code that generates and edits forms of users. The number of forms depend on number of users registered. As a developer, I don't know what's the number of users that can register per day.

The code is like this:

for($i=0;$i<$n;$i++) 
{
    echo "<form method='post'> 
    <input type='text' name='fname'>
    <input type='text' name='lname'>
    <input type='submit' name='submit' value='save'></form>";
} 

This code can repeat with 4 or 5 or ++ users. When i do:

if(isset($_POST['submit']))
{
 //code
}  

for recovering the value of the two inputs.

How does the PHP know the source of the event? It can make a mistake because all button has the same name? Please help me!

Upvotes: 1

Views: 34

Answers (1)

Wooz
Wooz

Reputation: 306

You can give the button different names then:

for($i=0;$i<$n;$i++) 
{
  echo "<form method='post'> 
  <input type='text' name='fname'>
  <input type='text' name='lname'>
  <input type='submit' name='submit".$i."' value='save'></form>";
} 

And then loop through the names to see if (and which) button is pressed:

for($i=0;$i<$n;$i++) 
{    
  if(isset($_POST['submit'.$i]))
  {
   //code
  }
}  

If in addition you want to distinguish post values of the inputfields you could index their names alike.

Upvotes: 1

Related Questions