我在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>
当检查时,应该在php页面onoff-switch.php上发送ON或OFF值。不需要第15行的两个问题,但是,除非存在,否则表单不会提交任何值。拿走第15行,当我选中或取消选中该框时,它应该自行提交。它没有。
此外,onoff-switch.php给出了值“OFF”的反馈,我不知道从哪里得到它,因为“OFF”在代码中没有任何地方。当然,如果未选中此框,则应为该值。
问题是这个复选框出错了吗?
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);
?>
答案 0 :(得分:2)
if($scanningvalue=='') $scanningvalue='off';
你添加了上面的代码,在查询之后你应该在设置$ scanningvalue的值之后添加它,还有一件事,你不需要mysql_real_escape_string,因为你设置的值并没有sql注入的问题
答案 1 :(得分:1)
如果您想打开/关闭,为什么不使用单选按钮而不是复选框?