Jump to content


Ulimited get variable from form to $_POST

  • Please log in to reply
1 reply to this topic

#1 RathanakBun

  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 25 April 2013 - 02:15 AM

for ($i=1;$i<10;$i++)

         if (isset($_POST['btnsubmit']))

              echo $fram."\t";
              echo $year."\t";
              echo $ton."\t";
              echo $model."\t";
              echo "<br>";




This code above will show variable till 9 but i want to show only sometime 2 or sometime 3 or sometime 5 how can i close other variable

what code i can use or what function ?


#2 Psycho

  • Moderators
  • Move along, nothing to see here
  • 11,846 posts
  • LocationCanada

Posted 25 April 2013 - 05:49 AM

First off, change your field names so the groups of fields will be in an array


Car 1:
<input type="text" name="cars[0][fram]" />
<input type="text" name="cars[0][year]" />
<input type="text" name="cars[0][ton]" />
<input type="text" name="cars[0][model]" />
Car 2:
<input type="text" name="cars[1][fram]" />
<input type="text" name="cars[1][year]" />
<input type="text" name="cars[1][ton]" />
<input type="text" name="cars[1][model]" />
. . . .etc.


Now, you can iterate over each group of fields in a more simple/logical manner

foreach($_POST['cars'] as $car)
    echo "{$car['fram']}\t{$car['year']}\t{$car['ton']}\t{$car['model']}\t<br>";


As for limiting the number of records you process - it all depends. If you limit the number of field sets that are created then the foreach() loop will work perfectly. Else, if you will always have 10 fieldsets but only want to process n, then you can use array_slice() to trim the array to the number of elements you want.

The quality of the responses received is directly proportional to the quality of the question asked.

I do not always test the code I provide, so there may be some syntax errors. In 99% of all cases I found the solution to your problem here: http://www.php.net

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users