0

Here i am retriving data from database and i am doing insert,update and delete of data using checkbox.My problem is when i click more than two checkbox than also i am moving to update.php page but what i actually want is that when i click on update button first it will check that only one checkbox is selected from list if not than alert message should display like select only one checkbox. please help.Thanks in advance Here i have put my code.

<!DOCTYPE html>
<html>
<head>
    <title>Insert Update Delete</title>
</head>
<body>
<form name="dataform" id="data" action="" method="get">
<?php
$connection = mysql_connect("localhost", "root", "");
$db = mysql_select_db("db_new", $connection);
$query=mysql_query("select * from student_info");
echo "<table>";
while($row=mysql_fetch_array($query))
{
    echo "<tr>";
    echo "<td>"; ?> <input type="checkbox" id="box" name="num[]" class="other" value="<?php echo $row["id"];?>" /> <?php echo "</td>";
    echo "<td>"; echo $row["roll_no"]; echo "</td>";
    echo "<td>"; echo $row["name"]; echo "</td>";
    echo "<td>"; echo $row["division"]; echo "</td>";
    echo "<td>"; echo $row["std"]; echo "</td>";
    echo "<td>"; echo $row["gender"]; echo "</td>";
    echo "<td>"; echo $row["subject"]; echo "</td>";
    echo "</tr>";
}
echo "</table>";
?>
    <input type="submit" name="insert" value="insert"/>
    <input type="submit" name="update" value="update"/>
    <input type="submit" name="delete" value="delete"/>
    </div>
</form>
<?php
if(isset($_GET["insert"]))
{
    $box = $_GET['num'];
    $count =count($box);
    if ($count == 0){
        header('Location:insert.php');
    }
    elseif($count == 1){
        header('Location:data.php');
    }
}
?>
<?php
if (isset($_GET['update'])){
    $box = $_GET['box'];
    $count =count($box);
    if($count == 0) {
        header('Location:data.php');
    }elseif($count == 1){
        header('Location:update.php');
    }
    if(!empty($_GET['num'])) {
        foreach($_GET['num'] as $id) {
            echo $id;
            header("location:update.php?id=".$id);
        }
    }
}

?>
<?php
if (isset($_GET['delete'])) {
    $box = $_GET['num'];
    $count = count($box);
    if($count == 0) {
        header('Location:data.php');
    }elseif($count == 1){
        header('Location:data.php');
    }
    if(!empty($_GET['num'])) {
        foreach($_GET['num'] as $id) {
            mysql_query("delete from student_info where id = $id");
        }
    }
}
?>
</body>
</html>
Ruchir shah
  • 11
  • 2
  • 8

4 Answers4

2

You could use a radio button for this instead of a checkbox.

see sample here

Use Radio Instead of CheckBox

Or if you must use a checkbox do this:

$('input.example').on('change', function() {
    $('input.example').not(this).prop('checked', false);  
});
Community
  • 1
  • 1
Neo
  • 3,309
  • 7
  • 35
  • 44
0

I would use a radio button, however I don't know your situation so here's a solution using jQuery. You want to select all of the checkboxes that are checked then see if the length of that array is greater than one. Here's the javascript code you'll need and a jsbin if you want to look at it.

http://jsbin.com/qakuzajaza/edit?js,output

$("#submitbutton").click(function() {

  var count = $("input[type=checkbox]:checked").length;
  if (count > 1) {
    alert("Only 1 can be selected");
  } else {
    // submit data
  }

});
Brandon
  • 3,074
  • 3
  • 27
  • 44
0
 1. Try this, it will not allow you to check only one checkbox at a time.

    $(document).ready(function () {
            $('input[type=checkbox]').click(function () {
                var chks = document.getElementById('#ckeckboxlistId').getElementsByTagName('INPUT');
                for (i = 0; i < chks.length; i++) {
                    chks[i].checked = false;
                }
                if (chks.length > 1)
                    $(this)[0].checked = true;
            });
        });
Bhanu Pratap
  • 1,635
  • 17
  • 17
0

The approaches are more or less similar. Try this:

submitForm(); //according to an event you prefer (click some button or anchor , no button over $ .ajaks.
function submitForm(){
    var tag = document.getElementById("tdForm");
    var chkbx = $(tag).find("input[type='checkbox']:checked").length;
    if (chkbx !== 0) {
        if (chkbx > 1) {
            alert("more then 1");
        }else{
            alert("form submited"); //$(tag).submit();
        }
    }else{
        if (chkbx === 0) {
            alert("nothing cecked");
        }
    }
};

Tested works for me. Hope it helps! While I think the radio buttons or previous validation, and interception of errors on the rules for completing the form, are much more affordable.

Borache
  • 23
  • 1
  • 5
  • Keep in mind what @adeneo said. "id" mast be a unique all around the same application, otherwise surely you will face a conflict once! Therefore we can use names or/and classes! – Borache Oct 14 '16 at 16:17