我之前从未真正使用过单选按钮。 所以我想要实现的是,使用两个单选按钮“公共”和“私人”。 这是针对用户的个人资料。 我的按钮的HTML代码是;
<div class="btn-group" data-toggle="buttons-radio"> <button type="button" class="btn btn-primary" id="privacy" name="privacy" value="0">Private</button> <button type="button" class="btn btn-primary" id="privacy" name="privacy" value="1">Public</button> </div>至于PHP,我不确定如何获取值并存储它。 任何帮助表示赞赏!
I have never really used radio buttons before. So what I am trying to achieve is, using two radio buttons 'Public' and 'Private'. This is for the user's profile. My HTML code for the buttons is;
<div class="btn-group" data-toggle="buttons-radio"> <button type="button" class="btn btn-primary" id="privacy" name="privacy" value="0">Private</button> <button type="button" class="btn btn-primary" id="privacy" name="privacy" value="1">Public</button> </div>As for the PHP, I'm not sure how to get the value and store it. Any help is appreciated!
最满意答案
使用表格。
<form method="get" action="yourscript.php"> <input type="radio" id="private" name="privacy" value="0"> <label for="private">Private</label> <input type="radio" id="public" name="privacy" value="1"> <label for="public">Public</label> <input type="submit" value="Save"> </form>将按钮更改为input元素,这些元素与表单一起提供了一种HTML本机方式,可以将数据发送到脚本而无需Javascript(尽管您可以在以后使用Javascript来增强可用性)。
使用label标签,您可以为radiobutton分配文本。 这样也可以点击这个文本,它也为屏幕阅读器提供了更好的支持,因为它们实际上可以看到哪些文本属于radiobutton。
id必须是唯一的,因此我将其更改为private和public 。 ID还用于将标签链接到。
该name确定将值发送到脚本的名称。
在PHP中,您可以使用超全局$_GET 。 $_GET['privacy']将包含'0'或'1'具体取决于所做的选择。 如果表单的方法是post ,你可以使用superglobal $_POST ,或者你可以使用$_REQUEST ,它包含来自其中任何一个的值,所以在这种情况下你的脚本不关心值是否在url中发送(获取)或与请求一起发送的不可见发布数据块。
Use a form.
<form method="get" action="yourscript.php"> <input type="radio" id="private" name="privacy" value="0"> <label for="private">Private</label> <input type="radio" id="public" name="privacy" value="1"> <label for="public">Public</label> <input type="submit" value="Save"> </form>Change your buttons to input elements, which, together with the form, provide a HTML native way to send data to a script without the need for Javascript (although you can use Javascript to enhance the usability later on).
Using the label tag, you can assign a text to a radiobutton. This allows this text to be clicked as well, and it also provides better support for screen readers, because they can actually see which text belongs to the radiobutton.
An id needs to be unique, so I changed it to private and public. The ids are also used to link the label to.
The name determines by which name the value is sent to your script.
In PHP, you can use the superglobal $_GET. $_GET['privacy'] will contain '0' or '1' depending on the choice made. If the method of the form was post, you could use the superglobal $_POST instead, or you can use $_REQUEST, which contains values from either, so in that case your script doesn't care whether the values were send in the url (get) or in the chunk of invisible post data that is sent along with the request.
更多推荐
buttons,value,class,按钮,HTML,电脑培训,计算机培训,IT培训"/> <meta name="d
发布评论