Multidimensional POST array giving me grief

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rhecker
Forum Contributor
Posts: 178
Joined: Fri Jul 11, 2008 5:49 pm

Multidimensional POST array giving me grief

Post 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> 
User avatar
Weiry
Forum Contributor
Posts: 323
Joined: Wed Sep 09, 2009 5:55 am
Location: Australia

Re: Multidimensional POST array giving me grief

Post 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.
rhecker
Forum Contributor
Posts: 178
Joined: Fri Jul 11, 2008 5:49 pm

Re: Multidimensional POST array giving me grief

Post 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> 

User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Multidimensional POST array giving me grief

Post 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>';
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply