4

I have two pages in php the first contain a form which is:

<form method="post" action="addnames.php">
   <input type="text" name="name" placeholder="Name" /><br />
   <input type="text" name="name" placeholder="Name" /><br />
   <input type="text" name="name" placeholder="Name" /><br />
   <input type="text" name="name" placeholder="Name" /><br />

   <input type="submit" value="Done" />
</form>

this takes the data to other php page where I am using foreach to read the request in this way:

 foreach($_REQUEST['name'] as $name){ 
     //MY CODE
 }

So what is the problem?

Hassan
  • 75
  • 5

3 Answers3

6

If you want to get name as an array then you need to change your form code:

<form method="post" action="addnames.php">
   <input type="text" name="name[]" placeholder="Name" /><br />
   <input type="text" name="name[]" placeholder="Name" /><br />
   <input type="text" name="name[]" placeholder="Name" /><br />
   <input type="text" name="name[]" placeholder="Name" /><br />

   <input type="submit" value="Done" />
</form>

Now you can get all names in your post request.

Hardik Solanki
  • 3,153
  • 1
  • 17
  • 28
1

Because only one name is sent to the server, and it's a string then not an array. To send an array of names, change your input name to name="name[]" to identify it as an array

<input type="text" name="name[]" placeholder="Name" />
...
Coloured Panda
  • 3,293
  • 3
  • 20
  • 30
1

Try this code:

<?php
foreach($_REQUEST['name'] as $name){ 
 //MY CODE
}?>

<form method="post" action="addnames.php">
   <input type="text" name="name[]" placeholder="Name" /><br />
   <input type="text" name="name[]" placeholder="Name" /><br />
   <input type="text" name="name[]" placeholder="Name" /><br />
   <input type="text" name="name[]" placeholder="Name" /><br />

   <input type="submit" value="Done" />
</form>
rdn87
  • 739
  • 5
  • 18