PHP:获取单选按钮的值并将其存储到数据库中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17730937/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP: Getting the value of a radio button and storing it into a database
提问by StyleOnPC
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;
我以前从未真正使用过单选按钮。所以我想要实现的是,使用两个单选按钮“公共”和“私人”。这是用于用户的个人资料。我的按钮 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>
As for the PHP, I'm not sure how to get the value and store it. Any help is appreciated!
至于PHP,我不确定如何获取值并存储它。任何帮助表示赞赏!
回答by GolezTrol
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).
将您的按钮更改为input
元素,这与表单一起提供了一种 HTML 原生方式来将数据发送到脚本,而无需使用 Javascript(尽管您可以在以后使用 Javascript 来增强可用性)。
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.
使用label
标签,您可以为单选按钮分配文本。这也允许单击此文本,并且还为屏幕阅读器提供了更好的支持,因为他们实际上可以看到哪个文本属于单选按钮。
An id
needs to be unique, so I changed it to private
and public
. The ids are also used to link the label to.
Anid
需要是唯一的,所以我将其更改为private
and public
。id 也用于将标签链接到。
The name
determines by which name the value is sent to your script.
该name
由命名值决定被发送到你的脚本。
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.
在 PHP 中,您可以使用超全局$_GET
. $_GET['privacy']
将包含'0'
或'1'
取决于所做的选择。如果表单的方法是post
,则可以改用超全局变量$_POST
,或者可以使用$_REQUEST
,它包含来自其中任何一个的值,因此在这种情况下,您的脚本不关心值是在 url (get) 中发送还是在与请求一起发送的不可见帖子数据块。
回答by Chathuranga Tennakoon
i have assumes that your form is something as follows.
我假设你的表格如下。
<form name="sample" action="form_submit.php" method="POST">
<div class="btn-group" data-toggle="buttons-radio">
<input type="button" class="btn btn-primary" id="private" name="privacy" value="0">Private</input>
<label for="private">Private</label>
<input type="button" class="btn btn-primary" id="public" name="privacy" value="1">Public</input>
<label for="public">Public</label>
</div>
<input type="submit" name ="submit" value ="Submit"/>
</form>
you can use the following code inside the action (form_submit.php) to get the selected radio button value.
您可以在操作 (form_submit.php) 中使用以下代码来获取选定的单选按钮值。
form_submit.php
form_submit.php
<?php
$selected_radio = $_POST['privacy'];
print $selected_radio;
?>
make sure that you are not duplicating the IDs because they should be unique.
确保您没有复制 ID,因为它们应该是唯一的。
回答by M Khalid Junaid
change type to radio
and donot use the same id for morethan one element id is supposed to be unique
将类型更改为radio
并且不要对多个元素使用相同的 id id 应该是唯一的
Also when you post the form type=button
values will not be passed with the form
此外,当您发布表单type=button
值时,不会随表单一起传递
<div class="btn-group" data-toggle="buttons-radio">
<input type="radio" class="btn btn-primary" name="privacy" value="0"/>Private
<input type="radio" class="btn btn-primary" name="privacy" value="1"/>Public
</div>
回答by exussum
assuming its posted on $_POST['privacy']
should contain either a 1
or a 0
假设其发布的内容$_POST['privacy']
应包含 a1
或 a0
also dont use an ID twice - they are supposed to be unique
也不要两次使用 ID - 它们应该是唯一的
回答by zavg
You don't have form
and submit
button.
你没有form
和submit
按钮。
HTML page:
HTML页面:
<div class="btn-group" data-toggle="buttons-radio ">
<form name="privacyForm" action="action.php" method="post">
<button type="radio" class="btn btn-primary" name="privacy" value="0">Private</button>
<button type="radio" class="btn btn-primary" name="privacy" value="1">Public</button>
<input type="submit" value="Submit">
</form>
</div>
action.php
动作.php
<?php
echo $_POST['privacy']; // should be 0 or 1
?>