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 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)