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!
I'm writing a PHP program. I've encountered a problem; in the following code I try to pass $_POST['delete'] which is an array as the value of a hidden input to some form, but it doesn't do so.
In fact, there's something wrong with converting PHP array into HTML array.
I'm sure that $_POST['delete'] is not null and is a real array.
I'm trying to prompt user to confirm the deletion of some values selected by checkboxes. There is a first form whose checkboxes' name is 'delete[]'(that must include brackets so that it can be treated as an array of values). Then that array is passed to a second form as a hidden input. The second form including a question message and a couple of buttons(Yes & No) uses the 'delete' array in order to remove the desired values from DB, if the user clicks 'Yes'.
The behavior of that is much like of phpMyAdmin's, when trying to delete some rows of a table.
/* Proccessing the Second Form Result */
if (isset($_POST['sure_submit'])) {
if (isset($_POST['delete'])) {
foreach ($_POST['delete'] as $user) {
$query = "DELETE FROM users_tbl WHERE username='$user'";
mysql_query($query);
}
}
}
/* The Second Form */
if (isset($_POST['submit'])) {
if (isset($_POST['delete'])) {
echo '<form action="'.$self.'" method="POST">';
echo 'Are you sure you want to delete the following user(s): <br />';
foreach ($_POST['delete'] as $user)
echo "$user<br />";
echo '<input type="submit" name="sure_submit" value=" Yes " />';
echo '<input type="submit" name="no" value=" No " />';
echo '<input type="hidden" name="delete[]" value="'.$_POST['delete'].'" />';
echo '</form>';
}
}
//remove the square brackets as the value you are using is already an array
//change
echo '<input type="hidden" name="delete[]" value="'.$_POST['delete'].'" />';
//to
echo '<input type="hidden" name="delete" value="'.$_POST['delete'].'" />';
I don't know what your first form looks like but this should work.