dou91808 2016-12-09 15:21
浏览 125
已采纳

从切换开关获取值不一致

i am trying to send the value of a toggle switch to my URL and retrieve it with Php. The code is working. The problem is every time i change the page or using the back-button i need to get the page reloaded so the toggle switch works, but when staying at the same page is doesnt work. what am doing wrong ?

This my code:

<?php
    $status_water = $_GET['water_swicth'];
    $status_light = $_GET['light_swicth'];
    $status_air   = $_GET['air_swicth'];

    if ($status_water == "on")
        { $water_on = "selected"; 
          $file = fopen("light.json", "w") or die("can't open file");
          fwrite($file, '{"water": "on", "light":off, "air":off}');
          fclose($file);
        }
    if ($status_water == "off") {  
      $file = fopen("light.json", "w") or die("can't open file");
          fwrite($file, '{"water": "off", "light":"off", "air":off}');
      fclose($file);
    }
    ?>
    <html>

    <head>
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" href="css/MyTheme.min.css" />
        <link rel="stylesheet" href="css/jquery.mobile.icons.min.css" />
        <link rel="stylesheet" href="css/jquery.mobile.structure-1.4.5.min.css" />
        <script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
        <script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
    </head>

    <body>
        <div data-role="footer">
            <div data-role="navbar">
                <ul>
                    <li style="font-size: 14px;">Water
                        <div id="water_switch">
                            <select name="water" id="water" data-role="flipswitch" data-mini="true">
                                <option value="off" myTag="off">Off</option>
                                <option value="on" myTag="on" <?php echo $water_on; ?>>On</option>
                            </select>
                        </div>
                    </li>
            </div>
        </div>
        </div>
        <script type="text/javascript">
            $(document).ready(function () {
                $('#water_switch').on('click', function () {
                    var checkStatus = $('option:selected', this).attr('mytag');
                    window.location.href = "stats.php?id=<?php echo $id; ?>&water_swicth=" + checkStatus;
                });
            });
        </script>
    </body>

    </html>
  • 写回答

1条回答 默认 最新

  • doushan1157 2016-12-09 17:00
    关注

    i got it to work, by moving the part from the to

    ref: https://forum.jquery.com/topic/script-not-running-unless-i-refresh-page

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

    报告相同问题?

    悬赏问题

    • ¥15 电脑锁屏时vba运行SAP,excel数据不能复制到SAP
    • ¥50 74LS系列 74LS00 74LS04设计一个RS485电路(关键词-差分)
    • ¥30 各位help写一下代码
    • ¥15 在运行SDEdit模型下载不了
    • ¥15 求51控制l298n驱动的小车中超声波避障怎么写
    • ¥15 电脑连上WIFI却用不了
    • ¥30 MATLAB在RLC电路的固有响应和阶跃响应GUI仿真报告
    • ¥15 hyper-v出现的问题
    • ¥15 有能用的可加酬金,求可以批量下载懒人听书的软件,能登录自己帐号的。
    • ¥100 高博一起做RGB-D SLAM(5)VO无法出visualisation问题