Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Loop through $_POST values

I have a very simple form on my website.

I run this PHP to loop the form contents:

echo '<pre>';
var_dump($_POST);
echo '</pre>';

$i = 0;

foreach($_POST as $key => $value)
{

    $i++;
    echo $value[$i]['row_id'];

}

I get the below:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

array(1) {
  ["data"]=> array(2) {
            [1]=>  array(2) {
                ["row_id"]=> string(5) "33714"
                ["sport"]=> string(8) "swimming"
            }
            [2]=> array(2) {
                ["row_id"]=> string(5) "33715"
                ["sport"]=> string(8) "football"
            }
  }
}

33714

My PHP only echoes the first row id 33714 instead of both rows.

I feel I’m missing something obvious here.

>Solution :

Simply put, you need to loop the items of $_POST['data'] instead of just $_POST

echo '<pre>';
var_dump($_POST);
echo '</pre>';

foreach($_POST['data'] as $key => $item)
{
    echo $key.':'. $item['row_id'];

}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading