Page 1 of 1

Multidimensional POST array giving me grief

Posted: Wed Mar 31, 2010 11:06 am
by rhecker
I have a form that is a list populated by every student name, course name, etc. and a checkbox from a database. I want to be able to check selected records in the list, then perform an action on the selected records upon POST. I understand that the result I get from POST is a multidimensional array, but I can't figure out how to extract the records. I am posting an abreviated version of the code below. I think the form is OK. It's extracting the POST data that is driving me nuts. Thanks for any thoughts.

Code: Select all

<?php
if ($_POST['submit']){
	
foreach($_POST [delete_student] as $d) {
echo $d[student_id][$row];
echo $d[student_id][$row];
}
}
?>

  <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
  <input  type="submit" name="submit" value="SUBMIT"/>
<?php
$eRow = 0; 		
        while ($user = mysql_fetch_assoc($sql)) {
			$iRow++;
<input name="delete_student[<?php echo $eRow ?>]" type="checkbox" value="1" />          
<?php echo $Name?>
<input name="student_id[<?php echo $eRow ?>]" type="hidden" value="<?php echo $user['student_id'];?>"/>
<input name="course_id[<?php echo $eRow ?>]" type="hidden" value="<?php echo $course_id ?>"/>         
        <?php
        }
        ?>
</form> 

Re: Multidimensional POST array giving me grief

Posted: Wed Mar 31, 2010 11:16 am
by Weiry
double check your <?php and ?> tags.. your missing one.

This should not have a space between $_POST and ['delete_student']
[delete_student] must also use ' ' so it would be ['delete_student'] as well as $d[student_id] requires the ' ' too.

Code: Select all

foreach($_POST [delete_student] as $d) {
echo $d[student_id][$row];
}
}
You have 2 separate variables..
$eRow and $iRow.
$iRow increments and $eRow does not.

Re: Multidimensional POST array giving me grief

Posted: Wed Mar 31, 2010 12:24 pm
by rhecker
Thanks for looking. I have changed the $iRow to $eRow and reposted here.

Code: Select all

<?php
if ($_POST['submit']){
        
foreach($_POST [delete_student] as $d) {
echo $d[student_id][$row];
echo $d[student_id][$row];
}
}
?>

  <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
  <input  type="submit" name="submit" value="SUBMIT"/>
<?php
$eRow = 0;              
        while ($user = mysql_fetch_assoc($sql)) {
                        $eRow++;
<input name="delete_student[<?php echo $eRow ?>]" type="checkbox" value="1" />          
<?php echo $Name?>
<input name="student_id[<?php echo $eRow ?>]" type="hidden" value="<?php echo $user['student_id'];?>"/>
<input name="course_id[<?php echo $eRow ?>]" type="hidden" value="<?php echo $course_id ?>"/>         
        <?php
        }
        ?>
</form> 


Re: Multidimensional POST array giving me grief

Posted: Wed Mar 31, 2010 12:48 pm
by AbraCadaver
This should clarify:

Code: Select all

if ($_POST['submit']){
   foreach($_POST['delete_student'] as $key => $value) {
      echo $value;
      echo $_POST['student_id'][$key];
      echo $_POST['course_id'][$key];
   }
}
//or just do
echo '<pre>'; print_r($_POST); echo '</pre>';