PHP-测试是否选择了单选按钮并获取值 [英] Php - testing if a radio button is selected and get the value

查看:75
本文介绍了PHP-测试是否选择了单选按钮并获取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用php.我想知道如何测试是否选择了单选按钮并获取值?我可以测试是否选择了单选按钮,但无法获取该值. 我创建了一个按钮以在表单中对此进行测试.首先,我选择一个单选按钮,然后单击该按钮,它必须显示一条消息,提示我选择了哪个值,并将此值放入变量中.为了测试是否选择了单选按钮,我这样做是:

I'm using php. I'd like to know how can I test if a radio button is selected and get the value? i can test if the radio button is selected but i cannot get the value. I created a button to test this in my form. First I select a radio button, then i click on the button and it must display a message that says which value i selected and put this value into a variable. In order to test if a radio button is selected i did like this:

$selected_radio=$_POST['SINGLE_' . $question->id . $multi_name_adjust . ''];
if ($selected_radio = 'checked'){}

谢谢

推荐答案

这很简单,请看下面的代码:

It's pretty simple, take a look at the code below:

表格:

<form action="result.php" method="post">
  Answer 1 <input type="radio" name="ans" value="ans1" /><br />
  Answer 2 <input type="radio" name="ans" value="ans2"  /><br />
  Answer 3 <input type="radio" name="ans" value="ans3"  /><br />
  Answer 4 <input type="radio" name="ans" value="ans4"  /><br />
  <input type="submit" value="submit" />
</form>

PHP代码:

<?php 

$answer = $_POST['ans'];  
if ($answer == "ans1") {          
    echo 'Correct';      
}
else {
    echo 'Incorrect';
}          
?>

这篇关于PHP-测试是否选择了单选按钮并获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆