提交后,无法取消禁用已从DB获取其值的复选框

时间:2017-05-01 07:04:41

标签: javascript php html mysql

我需要你对我的问题的帮助。我有两个带复选框的表,第一个显示来自DB的所有记录(使用MySQL),第二个表在我检查并从第一个表提交后得到数据。所以我想在第一个表中禁用我的复选框我检查并将数据提交到第二个表,但如果我想取消副本,我检查第二个表,这样第一个表中的复选框将被启用,第二个表中的数据将被擦除(因为它只是存储的临时变量另一个DB)。在这段代码中,我使用的是disable,但它无法解决问题在哪里?

// this code for checked the checkbox that be chosen first
session_start();
  if (count($_POST)){
  	//save choices to session
          $_SESSION['select_DB']=$_POST['select_DB'];
  }

  function was_checked($i) {
    if ($_SESSION['select_DB']){
  	  if(in_array($i,$_SESSION['select_DB']) ) {
  	     return "checked='checked'";
  	  }
    }
    return "";
  }

  ?>

 // this one if cancel the submit
<script>
  function openCB() {
      document.getElementById("balik_DB[]").disabled = false;
  }
  </script>
  
 
//i'm using this to disable checkbox after submit  
  <script>
  function closeCB() {
      document.getElementById("balik_DB[]").disabled = true;
  }
  </script>

   <!-- This code show table with checkbox that its value get from DB--> 
    <tbody>


          <?php
              include "db_connect.php";

              $sql_select = $dt_bas->prepare("SELECT id_pegawai, nm_pegawai, tmp_lahir FROM pegawai");
              $sql_select->execute();

              while($row = $sql_select->fetch(PDO::FETCH_ASSOC)){
                        $id =  $row["id_pegawai"]; // id
                        $nm =  $row["nm_pegawai"]; // employe name
                        $tmp = $row["tmp_lahir"]; // birthday
        ?>
            <tr>
            <td><input type="checkbox" name="select_DB[]" id="select_DB[]" value=<?php echo"$id";?> <?=was_checked($id)?>/></td>
            <td><?php echo "$id";?></td>
            <td><?php echo "$nm";?></td>
            <td><?php echo "$tmp";?></td>
            <td>
                <a href="#">Ubah</a>
                <a href="#">Detail</a>
            </td>
            </tr>

        <?php    } ?>
        </tbody>

<!--this the second table that get its value from the first table-->
<form>

      <table border="1">
              <thead>
                <tr>
                <th></th>
                <th>ID</th>
                <th>Nama</th>
                <th>TTL</th>
                <th>Aksi</th>
              </tr>
              </thead>
              <tbody>
                <?php
                // this code for get the data that is chosen from checkbox
                include "db_connect.php";

                if(isset($_POST['select_DB'])){
                   foreach($_POST['select_DB'] as $select=>$option_id){
                      $sql_select2 = $dt_bas->prepare("SELECT id_pegawai, nm_pegawai,  tmp_lahir FROM pegawai WHERE id_pegawai = '$option_id' ");
                      $sql_select2->execute();

                      while($row = $sql_select2->fetch(PDO::FETCH_ASSOC)){
                        $id =  $row["id_pegawai"]; // id
                        $nm =  $row["nm_pegawai"]; // employee name
                        $tmp = $row["tmp_lahir"]; // birth place
                        ?>

                    <tr>
                    <td><input type="checkbox" id="balik_DB[]" value="<?php echo"$id";?>"/></td>
                    <td><?php echo"$id";?></td>
                    <td><?php echo"$nm";?></td>
                    <td><?php echo"$tmp";?></td>
                    <td>
                          <a href="#">Ubah</a>
                          <a href="#">Detail</a>
                    </td>
                    </tr>
            <?php          }
                    }
                }
                ?>


              </tbody>
              <tfooter>
              </tfooter>
      </table>
  <input type="submit" value="Balik" onclick="openCB()"/>
</form>

1 个答案:

答案 0 :(得分:0)

id="balik_DB[]"中的问题,因为您从数据库中获取数据并动态创建复选框,以便当多个复选框具有相同的“id”(id应该是唯一的)时会发生什么。因此,而不是使用id,你需要使用class = "balik_DB[]"