Handling HTML Form Array with PHP

188

I'm having a really hard time trying to figure out how to handle this:
I'm trying to create two arrays, where I enter 4 quizzes for a class, and then loop through them in my PHP code.

<tr>
<td>ENGL110</td>
<td><input type="text" name="EnglArray[]"></td>
<td><input type="text" name="EnglArray[]"></td>
<td><input type="text" name="EnglArray[]"></td>
<td><input type="text" name="EnglArray[]"></td>
</tr>

<tr>
<td>MATH242</td>
<td><input type="text" name="MathArray[]"></td>
<td><input type="text" name="MathArray[]"></td>
<td><input type="text" name="MathArray[]"></td>
<td><input type="text" name="MathArray[]"></td>
</tr>

This is how I'm trying to retrieve it in my PHP code

$EnglArray = $_POST["EnglArray"];

$MathArray = $_POST["MathArray"];

print_r($EnglArray);

I can't figure out what I'm doing wrong, but it seems like my array is always empty, andprint_r doesn't output anything.

853

Answer

Solution:

This should work.

<?php
  foreach ($_POST as $key=>$value){
   if (!is_array($value)){
     echo "$key = $value<br />";
   } else {
     echo "$key is an array:<br />";
     foreach ($value as $valKey=>$valVal){
      echo "$valKey = $valVal<br />";
     }

   }
  }
?>

People are also looking for solutions to the problem: php - Replace hyphenated range expressions with all values which exist in the range

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.