Lemon
Lemon

Reputation: 199

Multiple CheckBox delete

I am having a hard time on creating a multiple checkbox ajax delete. I am an ajax noob.

    $sql = 'SELECT * FROM users';
$qry = mysql_query($sql);
$html = '';
$html .='<center>';
$html .= '<a href="" id="a-add">+ Add New User</a>';
$html .= ' || <a href="" id="a-delete"> Delete User</a>';
$html .= '<div id="div-data">';
$html .= '<table border="1" width="300px">';
$html .= '<tr>';    
$html .= '<th width="5px"><input type="checkbox" id="cb-checkall"></th>';   //all
$html .= '<th> Users</th>';         
//$html .= '<th>Delete</th>';
while($row = mysql_fetch_array($qry)) {
    $html .= '<tr align="center">';     
    $html .= '<td><input type="checkbox" name="checkall" class="checkall" value="'.$row['uid'].'"/></td>';   
    $html .= '<td>'.$row['fname'].'</td>';
    $html .= '</tr>';           
}
$html .= '</table>';
$html .= '</div>';
echo $html;

delete.php (with the use of implode)

    $deleteid = implode(",",$_POST['checkall']);
$fname = mysql_escape_string($_POST['firstname']);
$sql = "DELETE from  `bpo`.`users` (`uid` ,`fname`)
        WHERE UID IN (".$deleteid.")";

My data doesnt go to delete.php. idk why. I need help on deleting the checked rows when I push delete button and automatically reflects to my database. The change have to appear on screen without refreshing (AJAX) and with the use of implode. Thank you.

Upvotes: 0

Views: 111

Answers (1)

Halcyon
Halcyon

Reputation: 57709

In your form change name="checkall" to name="checkall[]" to allow for multiple values.


Keep in mind that if no options are checked $_POST['checkall'] will be undefined which will generate an Undefined index notice. Use isset or array_key_exists.

If $deleteid is empty (like array()) you query will be malformed, ie.

DELETE from  `bpo`.`users` (`uid` ,`fname`) WHERE UID IN ()
                                       // unexpected ) -- ^

This will give you an SQL error. IN clause may not be empty.

Upvotes: 1

Related Questions