mercredi 22 juillet 2015

Show error message when delete button is pressed without checking checkbox

I am struggling with this problem quite a while for now. It delete the row when it is checked and delete button is pressed. But I need to generate error message when delete button is pressed without checking checkbox either by jquery or using php functions.

Thanks in advance.

<table> 
    <tr><td><form action="" method="post" >
<table class="table table-bordered">
    <thead>
        <tr style="background-color:#404040; color: #F0F0F0 ;;">
            <td colspan=10 style="color:#fff;"><button class="btn form-control btn-info"> <a href="add.php">Add Data</a></button></td>

        </tr>
        <tr>
            <th>Select</th>
            <th>Bus Type</th>
            <th>Source</th>
            <th>Destination</th>
            <th>Departure Time</th>
            <th>Arrival Time</th>
            <th>Seats</th>
            <th>Date</th>
            <th>Price</th>  
            <th colspan="2">Edit</th>
        </tr>
    </thead>

    <?php 
        $query= "select * from bus";

        $sql= mysql_query($query);

        //echo $query;
        $count=mysql_num_rows($sql);
                /*var_dump($count)*/
        while($row=mysql_fetch_row($sql)){ 
            //var_dump($row);
            ?>
    <tbody>
        <tr>
            <td><input type="checkbox" name="checkbox[]"class= "checkbox-data "value="<?php echo $row[0];?>"></td>
            <td><?php echo $row[2]; ?></td>
            <td><?php echo $row[3]; ?></td>
            <td><?php echo $row[4]; ?></td>
            <td><?php echo $row[6]; ?></td>
            <td><?php echo $row[5]; ?></td>
            <td><?php echo $row[7]; ?></td>
            <td><?php echo $row[9];?></td>
            <td><?php echo $row[8]; ?></td>
            <td><a href="edit.php?id=<?php echo $row[0]; ?>" class="text-info">Edit</a> </td>


        </tr>
        <?php } ?>

        <tr  style="background-color:#404040; color: #F0F0F0 ";>
        <td colspan=10 style="color:#fff;"><input class="btn form-control btn-info" type="submit" name="delete" id= "delete-button" value="Delete Data"> </td>
        </tr>
    </tbody>
</table>

</form></td></tr>


    <?php 
    if(isset($_POST['delete'])){
        for($i=0; $i<$count; $i++){

            $del_id=$_POST['checkbox'][$i];
            /*var_dump($del_id);
                var_dump($_POST);
                echo $del_id."<br>";*/

            $sql="delete from bus where id=$del_id";
            echo $sql; 
            $query=mysql_query($sql);
            if($query){
                echo "<meta http-equiv=\"refresh\" content=\"0;URL=admin.php\">";
                echo "delete success";
            }else{
                echo "delete unsuccess".mysql_error();
            }

        }

    }
?>




Aucun commentaire:

Enregistrer un commentaire