0

I am trying to get the data from the form I created where I used an array to name them like:

<input type="text" name="fname[]" />
<input type="text" name="mname[]" />
<input type="text" name="lname[]" />

where the said form field are dynamically inserted or removed in the page.

On my script to get the values of the forms, I wrote something like:

$student = array(
     'fname' => '$_POST[fname]',
     'mname' => '$_POST[mname]',
     'lname' => '$_POST[lname]',
);

But when I used var_dump to see the values, each field will be indexed from zero and if it has repeating fields, it would be again be declared as another array inside an array.

What I wanted to do is to have an array with this stucture:

$student = array(
     array(
          'fname' => 'fname1',
          'mname' => 'mname1',
          'lname' => 'lname1'
     ),
     array(
          'fname' => 'fname2',
          'mname' => 'mname3',
          'lname' => 'lname2'
     )
);

I tried using a loop but I just fail again and again. Can anyone help me solve this problem?

Thank you in advance for your help.

3 Answers 3

2
<input type="text" name="student[]['fname']" />
<input type="text" name="student[]['mname']" />
<input type="text" name="student[]['lname']" />

no loops necessary. Your $_POST['student'] variable will automatically be the array you wanted to achieve.

EDIT: this isn't achieving the desired result. It's incrementing student for each field. adding an n value to the first set of brackets like student[n][fname] does achieve the desired result. I don't know how the script is written to dynamically generate these three fields on the fly, but if you can figure out how to add an n value you're golden.

Sign up to request clarification or add additional context in comments.

7 Comments

+1 This was what I was typing in as an answer as well. No need for it anymore.
Hi Stephan, I am a bit confused on how will I be able to get the values using $_POST[]
@Jag: do a var_dump( $_POST[ 'student' ] ); and you'll see the structure of the student fields.
I got this one: array(6) { [0]=> array(1) { ["fname"]=> string(6) "fname1" } [1]=> array(1) { ["mname"]=> string(6) "mname1" } [2]=> array(1) { ["lname"]=> string(6) "lname1" } [3]=> array(1) { ["fname"]=> string(6) "fname2" } [4]=> array(1) { ["mname"]=> string(6) "mname2" } [5]=> array(1) { ["lname"]=> string(6) "lname2" } } Does this have the same structure as I indicated above?
@Jag: see edit in my answer; you mentioned these fields were generated on the fly, so you'll need to figure out a way on the front-end to add a numeric value in the empty bracket, like student[0][fname]. Then you're all set. I tested it a few different ways and this was the only way to achieve the array you are looking for.
|
0
<?php

for($i=0; $i<count($_POST['fname']); $i++) {

$student[] = array(
         'fname' => $_POST['fname'][$i],
         'mname' => $_POST['mname'][$i],
         'lname' => $_POST['lname'][$i],
          );  
}

?>

2 Comments

Since my form field numbers vary from every submit, what would I do for me to stop the loop? Sorry Clint, I'm still very new in programming.
Stephans is much easier/better way of doing it but I updated my example with a loop in it incase it's the looping you're confused about.
0

Oops, I thought Stephan's answer would suffice, but rather, it should be the following I believe (can't test right now). Try it:

<input type="text" name="student[0]['fname']" />
<input type="text" name="student[0]['mname']" />
<input type="text" name="student[0]['lname']" />

<input type="text" name="student[1]['fname']" />
<input type="text" name="student[1]['mname']" />
<input type="text" name="student[1]['lname']" />

etc...

(note the added numeric indexes)

Then when you do a echo '<pre>' . print_r( $_POST[ 'student' ], true ); you should see the structure you are looking for.

1 Comment

I wanted to have the form fields added and removed on the fly so, I should figure out how to add student[n][fname] so on. Thanks anyways for tell me how to cleanly print the array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.