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?

Recommended Answers

All 3 Replies

Hi, assuming the method is $_POST, you get the equivalent of:

$_POST['subject'][] = 'subject 1';
$_POST['subject'][] = 'subject 2';
$_POST['subject'][] = 'subject 3';
# and so on ...

so you can loop the values:

if(is_array($_POST['subject']))
{
    foreach($_POST['subject'] as $subject)
    {
        # code
    }
}

When debugging and in doubt, print the index:

echo '<pre>';
print_r($_POST['subject']);
echo '</pre'>;

So you can realize better how to handle the input. For more information check the documentation:

A big thanks to you, JorgeM. I think this works! I have tried it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.