vendredi 29 avril 2016

POST checked and unchecked checkboxes with html php

Im having hard time to figure it out how to insert the checked box-es or unchecked into the database with php.

I tried many many different ways but none is working, i think im very close but can't figure it out the problem. Btw i work with javascript and never worked with php except this time.

index.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://ift.tt/nYkKzf">
<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="http://ift.tt/1oMJErh"></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>
</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('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);
         $checkboxes = $_GET['case'];
         
         echo "<script>console.log(" . $checkboxes.length . ");</script>";
         
         
         
         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(!empty($_POST['case'])){
           foreach($_POST['case'] as $case){
                  $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=submit id='save' value=Save>";
         include 'save.php';
         ?>
</body>
</html>

Also this code works very well to get the datas from the databse in one free host, but it doesnt work for saving.

Also separate php file i tryed but without success. save.php

  if(isset($_POST['submit'])){
  if(!empty($_POST['check_list'])) {

    $con = mysqli_connect('sql7.freemysqlhosting.net','sql7117068','GZqaZj69G9','sql7117068');
    if (!$con) {
       die('Could not connect: ' . mysqli_error($con));
    }
    mysqli_select_db($con, 'sql7117068');
    $checkbox = $_GET['case'];

      echo "<script>console.log(" . $checkboxes.length . ");</script>";

    foreach($_POST['case'] as $case){
          $sql = "UPDATE url SET checked = 1 WHERE number = " . $case;
          $result = mysqli_query($con,$sql);
        }
     }
      mysqli_close($con);
    }

?>

checkboxes




Aucun commentaire:

Enregistrer un commentaire