drgaeqdqiiyg14608 2018-12-02 14:20
浏览 96
已采纳

只有最顶级的交换机才能在我的网站上运行

I'm using HTML, PHP and Ajax to toggle switch in my website. However, only the top most switch is functional. The reason why I use PHP is to display results from my DB, and Ajax is there to toggle switch without reloading the website. Thanks in advance and comment for any questions!

Photo Here :D I have three rows in the DB. Data retrieve fine. Top button works!

ps : removed all classes for simplicity

About main.php and recipe.inc.php. They are separated because recipe.inc.php is used in many documents.

main.php

<?php
   $conn = mysqli_connect(localhost,****,****,loginsystem);
   //DB connection
   $query="SELECT * FROM `recipe` WHERE creator='$uid'";
   //SQL Query
   $results = mysqli_query($conn,$query);
   $array = array();
   //Array to save key column of the result in order
     while ($row = mysqli_fetch_assoc($results)) {
       for ($i = 0; $i < count($row[recipe_ID]); $i++) {
         $array[$i] = $row[recipe_ID];
         echo '<input type="hidden" id="recipe_ID" name="recipe_ID" value="';
         echo $array[$i];
         echo '">';
    //might confuse you. this is just to hand over recipe_ID to recipe.inc.php
         echo  '<li>';
         echo  '<label>';
           if($row[status]==1){
            echo  '<input type="checkbox" id="checkStatus" name="checkStatus" checked="">';
    //In case where row[status] is equal to 1. means it's ON
                    }else{
            echo  '<input type="checkbox" id="checkStatus" name="checkStatus">';
    //OFF otherwise
                    }
         echo  '<span class="switcher-indicator"></span>';
         echo  '</label>';
         echo  '</li>';
         }
     } 
   ?>

recipe.inc.php

    <?php            
    if(isset($_POST['checkStatus'])){
    $recipe_ID = $_POST['recipe_ID']);

    if($_POST['checkStatus']=='ON'){
    $status = 1; //to save in DB as boolean. if ON->1
    }else if($_POST['checkStatus']=='OFF'){
    $status = 0; //if OFF->0
    }

  $sql = "UPDATE `recipe` SET `status`=$status WHERE creator=$uid AND recipe_ID=$recipe_ID";
    //nev
  mysqli_query($conn,$sql);
    }
    ?>

and the Ajax part is saved in another js file. recipe.js

    $(document).ready(function() {
     $('#checkStatus').on('click', function() {
        var checkStatus = this.checked ? 'ON' : 'OFF';
        var recipe_ID = $("#recipe_ID").val();
        $.post("loginsystem/includes/recipe.inc.php", {
          "checkStatus": checkStatus,
          "recipe_ID": recipe_ID
        },
        function(data) {
            $('#checkStatus').html(data);
        });
     });
});
  • 写回答

1条回答 默认 最新

  • dsfb20227 2018-12-02 14:23
    关注

    That sounds like all the switches have the same ID. If that is the case, only the first one will work.

    Try changing your code to look like this:

    <?php
       $conn = mysqli_connect(localhost,****,****,loginsystem);
       //DB connection
       $query="SELECT * FROM `recipe` WHERE creator='$uid'";
       //SQL Query
       $results = mysqli_query($conn,$query);
       $cnt = 1;
       $array = array();
       //Array to save key column of the result in order
         while ($row = mysqli_fetch_assoc($results)) {
           for ($i = 0; $i < count($row[recipe_ID]); $i++) {
             $array[$i] = $row[recipe_ID];
             echo '<input type="hidden" id="recipe_ID-' .$cnt. '" name="recipe_ID" value="';   <============== HERE
             echo $array[$i];
             echo '">';
        //might confuse you. this is just to hand over recipe_ID to recipe.inc.php
             echo  '<li>';
             echo  '<label>';
               if($row[status]==1){
                echo  '<input type="checkbox" id="checkStatus-' .$cnt. '" name="checkStatus" checked="">'; //<============== HERE
        //In case where row[status] is equal to 1. means it's ON
                        }else{
                echo  '<input type="checkbox" id="checkStatus-' .$cnt. '" name="checkStatus">'; //<================ HERE
        //OFF otherwise
                        }
             echo  '<span class="switcher-indicator"></span>';
             echo  '</label>';
             echo  '</li>';
             }
         $cnt++; <=========== HERE
         } 
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装