php - 使用html php POST选中和未选中的复选框

标签 php html mysql checkbox html-table

我很难弄清楚如何使用 php 将选中的框或未选中的框插入到数据库中。

我尝试了很多不同的方法,但都没有用,我想我已经很接近了,但无法解决问题。 顺便说一句,我使用 Javascript,除了这次以外从未使用过 PHP。

index.html

<html>
   <head>
      <style type="text/css">
         @import "demo_page.css";
         @import "header.ccss";
         @import "demo_table.css";
         @import "select.dataTables.min.css";
         @import "jquery.dataTables.min.css";
      </style>
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      <script type="text/javascript" charset="utf-8" src="jquery.js"></script>
      <script type="text/javascript" charset="utf-8" src="jquery.dataTables.js"></script>
      <script type="text/javascript" charset="utf-8" src="RowGroupingWithFixedColumn.js"></script>
      <script>$(document).ready(function(){load_(); console.log('load running')});</script>
   </head>
   <body id="dt_example">
      <table cellpadding="0" cellspacing="0" border="0" class="display" id="endpoints">
      <thead>
         <tr>
            <th></th>
            <th>Nr.</th>
            <th>Java Class Name</th>
            <th>http Method</th>
            <th>URL</th>
         </tr>
      </thead>
      <tbody>
         <?php
            $con = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
            if (!$con) {
                die('Could not connect: ' . mysqli_error($con));
            }
            mysqli_select_db($con,DB_NAME);
            $sql='SELECT * FROM url';
            $result = mysqli_query($con,$sql);


            while($row = mysqli_fetch_array($result)) {
              print $row['method'];
              switch ($row['http_method']) {
                            case "GET":
                                echo "<tr class='gradeA'>";
                                break;
                            case "PUT":
                                  echo "<tr class='gradeC'>";
                                break;
                            case "POST":
                                  echo "<tr class='gradeU'>";
                                break;
                            case "DELETE":
                                  echo "<tr class='gradeX'>";
                                break;
                            default:
                                  echo "<tr>";
                        }
              if($row['checked']){
                echo "<td><input type='checkbox' id=case name='case[]' value='" . $row['number']  . "' checked> </td>";
              } else {
                echo "<td><input type='checkbox' id=case name='case[]' value='" . $row['number']  . "'> </td>";

              }
                echo "<td align=center >" . $row['number'] . "</td>";
                echo "<td align=center >" . $row['class_name'] . "</td>";
                echo "<td>" . $row['http_method'] . "</td>";
                echo "<td style='font-weight:bold'>" . $row['endpoint']  . "</td>";
                echo "</tr>";

            }

            if(isset($_POST['save'])){
              echo "<script>console.log(" . $checkboxes.length . ");</script>";

              $rows = $_POST['case'];
              foreach($rows as $row){

                $sql = "UPDATE url SET checked = 1 WHERE number = " . $case;
                $result = mysqli_query($con,$sql);
              }
            }


            mysqli_close($con);
            echo "</tbody></table>";
            echo "<input type='submit' name='save' id='save' value='Save' />";
            ?>
   </body>
</html>

如有任何帮助,我们将不胜感激。

图片:

checkboxes

编辑: 已解决 - 这是代码:

<?php
if(isset($_POST['save'])){
  echo "<script>console.log(" . $checkboxes.length . ");</script>";
  $con = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
  if (!$con) {
      die('Could not connect: ' . mysqli_error($con));
  }
  mysqli_select_db($con,DB_NAME);

  $rows = $_POST['case'];
  foreach($rows as $row){

    $sql = "UPDATE url SET checked = 1 WHERE number = " . $row;
    $result = mysqli_query($con,$sql);
  }
}
?>
<!DOCTYPE html>
<html>
<head>
   <style type="text/css">
      @import "demo_page.css";
      @import "header.ccss";
      @import "demo_table.css";
      @import "select.dataTables.min.css";
      @import "jquery.dataTables.min.css";
   </style>
   <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
   <script type="text/javascript" charset="utf-8" src="jquery.js"></script>
   <script type="text/javascript" charset="utf-8" src="jquery.dataTables.js"></script>
   <script type="text/javascript" charset="utf-8" src="RowGroupingWithFixedColumn.js"></script>
   <script>$(document).ready(function(){load_(); console.log('load running')});</script>
</head>
<body id="dt_example">
  <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">

   <table cellpadding="0" cellspacing="0" border="0" class="display" id="endpoints">
   <thead>
      <tr>
         <th></th>
         <th>Nr.</th>
         <th>Java Class Name</th>
         <th>http Method</th>
         <th>URL</th>
      </tr>
   </thead>
   <tbody>
      <?php
         $con = mysqli_connect('sql7.freemysqlhosting.net','sql7117068','GZqaZj69G9','sql7117068');
         if (!$con) {
             die('Could not connect: ' . mysqli_error($con));
         }
         mysqli_select_db($con,'sql7117068');
         $sql='SELECT * FROM url';
         $result = mysqli_query($con,$sql);


         while($row = mysqli_fetch_array($result)) {
           print $row['method'];
           switch ($row['http_method']) {
                         case "GET":
                             echo "<tr class='gradeA'>";
                             break;
                         case "PUT":
                               echo "<tr class='gradeC'>";
                             break;
                         case "POST":
                               echo "<tr class='gradeU'>";
                             break;
                         case "DELETE":
                               echo "<tr class='gradeX'>";
                             break;
                         default:
                               echo "<tr>";
                     }
           if($row['checked']){
             echo "<td><input type='checkbox' id=case name='case[]' value='" . $row['number']  . "' checked> </td>";
           } else {
             echo "<td><input type='checkbox' id=case name='case[]' value='" . $row['number']  . "'> </td>";

           }
             echo "<td align=center >" . $row['number'] . "</td>";
             echo "<td align=center >" . $row['class_name'] . "</td>";
             echo "<td>" . $row['http_method'] . "</td>";
             echo "<td style='font-weight:bold'>" . $row['endpoint']  . "</td>";
             echo "</tr>";

         }




         mysqli_close($con);
         echo "</tbody></table>";
         echo "<input type='submit' name='save' id='save' value='Save' />";
         echo "</form>";
         ?>

</body>
</html>

PS:代码中可能有很多漏洞和糟糕的编程风格,但要知道我不是 PHP 开发人员,如果我的服务器受到不同的攻击也没关系。我只想解决问题并继续前进。没有时间在每一点都停下来。

最佳答案

使用下面的代码检查复选框是否被选中。然后在一些变量上设置一些标志以相应地将数据插入到数据库中。

//html
     <input type='checkbox' name='boxname' value='1' />
//php
<?php
    if(isset($_POST['boxname'])
    {
        echo "check box is checked";
    }
?>

关于php - 使用html php POST选中和未选中的复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36936123/

相关文章:

javascript - 如何在页面上单击按钮时将 css 与原生 js 一起应用

php - 通过return url获取买家姓名和收货地址

mysql - 连接表 : case statement for no matches?

mysql - 按非空值分组

php - 从结果计算日期的最佳方法?

php - 选择字段包含可能值的数据

php - 第二个准备好的声明没有触发

Php MySQL 最近 100 条记录最喜欢的查询

PHP 或 HTML 优先还是重要?

mysql - 是否可以在 INSERT 子查询中选择列名?