Accessing and collecting post[] data from an array in PHP

They have: 4 posts

Joined: Aug 2014

I have a form like the one below which is posted to processForm.php, and the user can dynamically add more with jquery.

<input type="text" name="subject[]" />
<input type="text" name="grade[]" />
<input type="text" name="year[]" />

<input type="text" name="subject[]" />
<input type="text" name="grade[]" />
<input type="text" name="year[]" />

<input type="text" name="subject[]" />
<input type="text" name="grade[]" />
<input type="text" name="year[]" />

Please How can i access those arrays from subject[], grade[] and year[] inputs by the user?

Greg K's picture

He has: 2,145 posts

Joined: Nov 2003

Assuming using POST instead of GET

$_POST['subject'][0]
$_POST['grade'][0]
$_POST['year'][0]
$_POST['subject'][1]
$_POST['grade'][1]
$_POST['year'][1]
$_POST['subject'][2]
$_POST['grade'][2]
$_POST['year'][2]

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.