【问题标题】:How to use Bootstrap Toggle in PHP?如何在 PHP 中使用 Bootstrap 切换?
【发布时间】:2016-02-16 15:05:24
【问题描述】:
我有一个复选框,我想看起来像 http://www.bootstraptoggle.com/
如果我有这样的 html 输入字段:<input checked data-toggle="toggle" type="checkbox">,它看起来真的是这样。
但是我应该如何使用它来将值 1/0 发布到我的 PHP 脚本中,就像它是一个真正的复选框一样?
我是否需要在 html 代码中添加其他内容,以及如何在 PHP 脚本中进行处理?
提前致谢。
【问题讨论】:
-
它应该仍然像一个普通的复选框一样工作。唯一的区别是它的外观。像this 这样的东西应该可以工作。
标签:
php
twitter-bootstrap
checkbox
toggle
【解决方案1】:
您可能希望为输入设置一个值,例如
value='1' or value='true'
<input checked data-toggle="toggle" type="checkbox" value='1' or value='true' name='mycheckbox' >
由于它是一个复选框,如果选中,它将在您提交表单时与您的其他表单数据一起使用...
然后在你的 php 端你只是做这样的事情
isset($_POST['mycheckbox'])
然后这意味着它已被检查...您以后可以捕获该值(如果您需要它来做某事)或您需要做的任何事情..
【解决方案2】:
您需要做的就是在输入中添加name 和value 属性。该插件已经为您完成了剩下的工作。
像这样:
<link href="https://gitcdn.github.io/bootstrap-toggle/2.2.0/css/bootstrap-toggle.min.css" rel="stylesheet">
<script src="https://gitcdn.github.io/bootstrap-toggle/2.2.0/js/bootstrap-toggle.min.js"></script>
<form method="post" action="submit.php">
<input type="checkbox" name="my_input" checked data-toggle="toggle">
<input type="submit">
</form>
结果为@987654324@:
var_dump($_POST);
/*
array (size=1)
'my_input' => string 'on' (length=2)
*/