按下删除按钮时显示错误消息而不选中复选框

时间:2015-07-23 05:02:49

标签: php jquery checkbox

我现在正在努力解决这个问题。它在选中时删除该行并按下删除按钮。但是我需要在按下删除按钮时生成错误消息而不通过jquery或使用php函数来检查复选框。

<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();
            }

        }

    }
?>

2 个答案:

答案 0 :(得分:0)

我认为这就是你要做的。你的表单有一个包含链接的按钮的方式有点奇怪,但是,这是一个简单的复选框验证w / message。为了清晰起见,我注意到了jQuery:

<script type="text/javascript" src="https://code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript" src="https://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
<form action="" method="post">
    <input type="checkbox" name="checkbox[]" class="checkbox-data" value="1">
    <input type="checkbox" name="checkbox[]" class="checkbox-data" value="2">
    <input type="checkbox" name="checkbox[]" class="checkbox-data" value="3">
    <input type="checkbox" name="checkbox[]" class="checkbox-data" value="4">
    <input type="checkbox" name="checkbox[]" class="checkbox-data" value="5">
    <input class="btn form-control btn-info" type="submit" name="delete" id= "delete-button" value="Delete Data">
</form>
<div id="tester"></div>
<script>
    // On submission via delete button
    $("input[name='delete']").click(function() {
        // Loop through each checkbox
        $.each($("input[type='checkbox']"),function() {
            // Check if this particular one is checked
            var IsChecked   =   $(this).is(":checked");
            // If there is at least one checked, submit form
            if(IsChecked != false)
                $(this).parents("form").submit();
        });
        // Default is to write an error
        $("#tester").html("You have not checked anything to delete.");
        return false;   
    });
</script>

答案 1 :(得分:0)

这样做

&#13;
&#13;
function delete1() {
  var v = false;
  $('input[type="checkbox"]:checked').each(function() {
    v = true;
    //do some ajax work
  });

  if (v == false) {
    alert("please check");
  }
  $('input[type="checkbox"]:checked').closest("tr").remove();
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="t1">
  <tr>
    <td>
      <input type="checkbox" />
    </td>
    <td>row 1
      <td>
  </tr>
  <tr>
    <td>
      <input type="checkbox" />
    </td>
    <td>row 2
      <td>
  </tr>
  <tr>
    <td>
      <input type="checkbox" />
    </td>
    <td>row 3
      <td>
  </tr>
  <tr>
    <td>
      <input type="checkbox" />
    </td>
    <td>row 4
      <td>
  </tr>
</table>
<input type="button" value="delete" id="d" onclick="return delete1();" />
&#13;
&#13;
&#13;