![](/img/trans.png)
[英]How can I check if a radio button is selected using JavaScript and PHP
[英]How can I know which radio button is selected in php?
这是我的 html 单选表单:
<form action="form2.php" method="POST">
<label><input type="radio" name="select" value="insert"> Insert </label>
<label><input type="radio" name="select" value="remove"> Remove </label>
</form>
如何检查 php 文件中选择了 2 个收音机中的哪一个(与数据库连接)。 例如,如果选择插入,则添加新成员。 如果选择删除,则删除成员。
$select = $_REQUEST['select'];
echo $select;
它返回“on”而不是插入/插入或删除/删除
这很简单。 只需检查您的帖子数据的价值。
<?php
if(isset($_REQUEST['select'])){
switch ($_REQUEST['select']) {
case 'insert':
// Insert new
break;
case 'remove':
// Remove
break;
}
}
为您的无线电输入添加一个value
属性:
<form action="form2.php" method="POST">
<label><input type="radio" name="select" value="insert"> Insert</label>
<label><input type="radio" name="select" value="remove"> Remove</label>
<button>submit</button>
</form>
我还添加了一个提交按钮。
如果form2.php
包含
<?php
echo $_REQUEST['select'];
或者
<?php
echo $_POST['select'];
根据您选择的收音机,它会在您提交表格后打印出insert
或remove
。
有关无线电输入的更多信息: https ://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/radio
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.