Jump to content

checkboxs, processing.


daydreamer

Recommended Posts

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.