0

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, and print_r doesn't output anything.

5
  • Try first doing print_r($_POST) to see what you're getting overall. Commented Mar 28, 2013 at 2:33
  • What is the method of your form? Are you using POST or GET? Commented Mar 28, 2013 at 2:34
  • did you use <form> tag in the beginning of your code? Commented Mar 28, 2013 at 2:42
  • the <form> tag definitely works, and I'm using POST Commented Mar 28, 2013 at 2:49
  • so print_r($_POST) returns: Array ( [Engl1] => 90 [Engl2] => 39 [Engl3] => 34 [Engl4] => 25 [Math1] => 34 [Math2] => 34 [Math3] => 53 [Math4] => 93 [Cisc1] => 34 [Cisc2] => 93 [Cisc3] => 43 [Cisc4] => 95 ) I also have a Cisc array Commented Mar 28, 2013 at 2:51

1 Answer 1

0

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 />";
     }

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

Comments

Your Answer

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