daydreamer Posted September 2, 2008 Share Posted September 2, 2008 Ok. I have a table list which is automatically generated from a mysql database. Beside each row is a checkbox. It has the value of each row ID. The user can click each row, then click delete to delete them from the database. <?php for($i=0;$i<=$checkbox;$i++){ //$checkbox= totalcheckboxes on page if ( isset($_POST['cb'.$i]) ) { // 'cb'.$i' = checkbox name/ID $deletearray[$i] = $_POST['cb'.$i]; } } ?> $deletearray is an array of all the checkbox ID's to delete. The problem is, if the checkbox is not selected, the code above still stores an empty variable. So if theres 6 checkboxes, and you select the 5th one, the array will hold: 0= 1= 2= 3= 4= 5=116 6= I want it to hold: 0=116 I have tried if ( !($_POST['cb'.$i] ==NULL) ) { But still the same problem. Thanks. Link to comment https://forums.phpfreaks.com/topic/122409-checkboxs-processing/ Share on other sites More sharing options...
Ken2k7 Posted September 2, 2008 Share Posted September 2, 2008 Try this: Replace $deletearray[$i] = $_POST['cb'.$i]; With $deletearray[] = $_POST['cb'.$i]; Link to comment https://forums.phpfreaks.com/topic/122409-checkboxs-processing/#findComment-632062 Share on other sites More sharing options...
The Little Guy Posted September 2, 2008 Share Posted September 2, 2008 Something like this: <form action="myPHP.php" method="post"> <input type="checkbox" name="delete[]" value="3" /> <input type="checkbox" name="delete[]" value="4" /> <input type="checkbox" name="delete[]" value="6" /> <input type="checkbox" name="delete[]" value="34" /> <input type="submit" value="Delete Selected" /> </form> foreach($_POST['delete'] as $delete){ $sql = mysql_query("SELECT * FROM table WHERE id='$delete'"); if(mysql_num_rows($sql) > 0){ mysql_query("DELETE FROM table WHERE id='$delete'"); } } header("Location: backToPage.php"); exit; Link to comment https://forums.phpfreaks.com/topic/122409-checkboxs-processing/#findComment-632065 Share on other sites More sharing options...
daydreamer Posted September 2, 2008 Author Share Posted September 2, 2008 cheers Ken2k7, thats exactly where im going wrong, should of seen it myself! The Little Guy thanks, i do have something very similar to that, but that part of the script wasn't the problem! Link to comment https://forums.phpfreaks.com/topic/122409-checkboxs-processing/#findComment-632072 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.