0

I have a simple checkbox function in javascript which would determine which items are checked.

<script type="text/javascript" language="javascript">
    var items = new Array();
    function prepareDeleteItems(className){
        var value = className;
        if($('.'+value).attr('checked'))
        {
            items[items.length] = value;
        }
        else
            removeItemFromList(value);
        reAddHiddenInput();
    }

    function reAddHiddenInput(){
        $('.itemsToDelete').html("");
        for(var i = 0; i<items.length; i++)
        {

            $('.itemsToDelete').append('<input type="hidden" name="itemstodelete[]" value="'+($('.'+items[i]).val())+'" />');
        }       
    }

    function removeItemFromList(className){
        for(var i = 0; i<items.length; i++)
        {
            if(items[i] == className)
                items.splice(i,1);
        }
        return;
    }
</script>

then I have the if statement which will delete the items selected.

if (isset($_POST['delete'])){

        $delete = $_POST['check'];

            $N = count($delete);
                echo $N;
                    for($i=0; $i < $N; $i++){

                        $strSQL = "DELETE FROM customerinfo ";  
                        $strSQL .="WHERE CUS_ID = '".$delete[$i]."' ";  
                        mysql_query($strSQL);  

                    }
    }

and to display the data

<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST">

<div>
<input type="button" title="delete item" value='Delete' name='delete'/></div>

<div>

<table width='95%' border='1'  class='hovertable'>
  <tr>
  <td colspan='9'>CUSTOMERS</td>
  </tr>
  <tr>
  <th>---</th>
  <th>---</th>
  <th>ID</th>
  <th>First Name</th>
  <th>Last Name</th>
  <th>Email</th>
  <th>Gender</th>
  <th>Address</th>
  <th>Contact</th>
  </tr>
<?php
while($row = mysql_fetch_array($sql2)){ 

$CUS_ID = $row['CUS_ID'];
$FNAME = $row['FNAME'];
$LNAME = $row['LNAME'];
$EMAIL = $row['EMAIL'];
$GENDER = $row['GENDER'];
$ADDRESS = $row['ADDRESS'];
$CONTACT = $row['CONTACT'];

 ?>
<tr>

<?php if (isset($_POST['update'])){ ?>
<?php if($ID==$CUS_ID){?>

<?php }else{?>

<?php } ?>
<?php }else{?>

    <td align='center'>

now this is where I put the checkbox.

<input type="checkbox" name="check[]" class="item_del_<?php echo $CUS_ID; ?>" value="<?php echo $CUS_ID; ?>" onclick="prepareDeleteItems('item_del_<?php echo $CUS_ID; ?>');" />
    <!--ADD-->        </td>                 
<?php } ?>
</form>     

and more codes.

but the checkbox function is not working, the items aren't deleted. please help.

1
  • <input type="button" title="delete item" value='Delete' name='delete'/></div> the error was here. i changed the input type into image. but i want to use a button? is there a solution? Commented Sep 10, 2012 at 16:04

1 Answer 1

1

i would do everything php-wise

<input type="checkbox" name="check[]"

could become

<input type="checkbox" name="check[<?php echo $CUS_ID; ?>]"

that way you would receive an array with items deleted with their ID as key

foreach ($_POST[check] as $key=>$value){
..delete element with id=$key here...
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.