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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.