/    Sign up×
Community /Pin to ProfileBookmark

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

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?

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@NogDogSep 08.2014 — One way:
[code=php]
foreach($_POST['subject'] as $ix => $subject) {
if(isset($_POST['grade'][$ix]) and isset($_POST['year'][$ix])) {
// do stuff with $subject, $_POST['grade'][$ix], and $_POST['year'][$ix]
}
else {
// any error processing here for missing grade/year fields
}
}
[/code]
Copy linkTweet thisAlerts:
@erabxesauthorSep 08.2014 — Okay! That really works! Thanks!

I have another question:

But how do i loop through this and send it into a database let say that i want to create a query that will not just accommodate three entries but as much as possible based on the number of entries?
×

Success!

Help @erabxes spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...