I've having some problems trying to get a section of code to work concerning checkboxes in php. here's my code to create the checkboxes:

<?php
include "header.php";

function main($plb_id) {
	global $xoopsDB, $xoopsUser;
	$result = $xoopsDB->query("select * from ".$xoopsDB->prefix('box_plbu')." where plb_id = $plb_id");
	$getRowsNum = $xoopsDB->getRowsNum($result);
	$count = 0;
	if ($getRowsNum > 0) {
		?>
		<html>
			<body background="images/pop_bg.gif">
				<form name="remove" id="remove" action="plb_members.php?func=remove" method="post">
				<table background="images/pop_bg.gif">
					<tr>
						<td>Name</td>
						<td>Remove</td>
					</tr>
		<?php
		while (list($plbu_id, $plb_id, $trail_name) = $xoopsDB->fetchRow( $result)) {
			echo "<tr>\n";
			echo "	<td>$trail_name</td>\n";
			echo "	<td><input type=\"checkbox\" name=\"tbr[]\" value=\"$plbu_id\"></td>\n";
			echo "</tr>\n";
			$count++;
		}
		echo "<tr>\n";
		echo "	<td><input type=\"submit\" name=\"remove\" id=\"remove\" value=\"Remove\">&nbsp;<input type=\"hidden\" name=\"count\" id=\"count\" value=\"$count\"></td>\n";
		echo "</tr>\n";
	} else {
	?>
	<html>
		<body background="images/pop_bg.gif">
			<table background="images/pop_bg.gif">
				<tr>
					<td>None</td>
					<td>&nbsp;</td>
				</tr>
	<?php
	}
	?>
			</table>
		</body>
	</html>
<?php
}

function remove() {
	$count = $GLOBALS['count'];
	if(is_array($_POST["tbr"])){
		foreach($_POST["tbr"] as $d){
			array_push($queries,"DELETE FROM table WHERE id=" . intval($d));
		}
	}
}

switch($func) {
    default:main($plb_id); break;
    case "remove":remove(); break;
}
?>

That works just fine, pulling the names from the database and displaying them with a checkbox by thier side.

The next part is that in the remove function, I want to loop through the array and if the checkbox is checked, delete the corresponding entry from the database. The current remove function is just something I scarffed fro the Net.

So, can anyone help me figure this one out?

    Firstly welcome to the forums. hope you enjoy yourself 🙂

    Umm about your question, not too sure on what exactly the problem is. Are you receiving errors?

      No, actually the problem is that I do not know how to loop through an array of checkboxes. I want the user to be able to select any number of checkboxes and then click on the submit button and delete the entries in the database that corresponds to the entries that the user checked the checkbox on.

      The ideal would be, I display the list of users with checkboxes, you checkmark the ones you want to preform an action on and then click on submit, I loop through the arry of checkboxes and perform the requested action on those corresponding entry in the database.

        The easiest way to set this up, is to call name all of your checkboxes something like:

        <input type='checkbox' name='boxes[]' value='0'>
        

        This way, when the form is submitted, the $_POST global will hold an array called 'boxes'. You can loop through it something like this...

        if ( is_array($_POST['boxes']) )
        {
           foreach ( array_values($_POST['boxes']) as $box )
           {
              //do something with $box
           }
        }
        

        It's important to check that it is an array, or if they don't check any, you'll get an error about such and such not being a valid array sent to foreach().

        Hope this helps.

          That's what I was looking for!!!!

          Thanks bunches!

            Write a Reply...