简体   繁体   English

复选框/开关无法正常运行

[英]Checkbox / Switch not functioning properly

I have a switch on a php page with the code: 我在带有代码的php页面上进行了切换:

<form action="onoff-switch.php" method="post">
<div class="onoffswitch">
    <input type="checkbox" 
               name="onoffswitch" 
               class="onoffswitch-checkbox" 
               id="myonoffswitch" checked 
               value="on">
    <label class="onoffswitch-label" for="myonoffswitch">
        <div class="onoffswitch-inner"></div>
        <div class="onoffswitch-switch"></div>
    </label>
</div>
 <input type="submit" name="formSubmit" value="Submit" />
</form>

which when checked is supposed to send a value of ON or OFF the the php page onoff-switch.php. 当选中该选项时,应该发送php页面onoff-switch.php的ON或OFF值。 Two problems, Line 15 is not needed, however, the form does not submit any value unless it is there. 有两个问题,不需要行15,但是,除非存在表单,否则表单不会提交任何值。 Taking away line 15, when I check or uncheck the box, then it is supposed to submit by itself. 删除第15行,当我选中或取消选中该框时,它应该自己提交。 It does not. 它不是。

Additionally, the onoff-switch.php gives the feedback of a value "OFF", which I have no clue where it gets it from since "OFF" is nowhere in the code. 另外,onoff-switch.php给出了值“ OFF”的反馈,由于“ OFF”在代码中不存在,因此我不知道从何处获取值。 Of course that should be the value if the box is not checked. 当然,如果未选中该框,则应为该值。

Question is where is this checkbox going wrong? 问题是此复选框在哪里出错?

PHP Code: PHP代码:

<?php
$con=mysqli_connect("localhost","user","pass","db");
// Check connection
if (mysqli_connect_errno())
{
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}


if (isset($_POST['formSubmit']))
{
    // escape variables for security
    $scanningvalue = mysqli_real_escape_string($_POST['onoffswitch']);

    $sqlon="INSERT INTO configuration (scanning) VALUES ('$scanningvalue')";
    $result=mysqli_query($con, $sqlon);
    if($scanningvalue=='') $scanningvalue='off';
       if($result)
    {
       echo "The db operation done (1 record added) and switch value ".$scanningvalue;
    }
else
{
   echo "The db operation error and switch value ".$scanningvalue;
}

}

mysqli_close($con);
?> 
if($scanningvalue=='') $scanningvalue='off';

您已在上面添加了代码,在查询之后,应改为在设置$ scanningvalue值和其他内容之后添加它,不需要mysql_real_escape_string,因为您正在设置该值,因此没有SQL注入的问题。

如果要打开/关闭,为什么不使用单选按钮而不是复选框?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM