如何获得PHP中单选按钮的值? [英] How do I get the value of a radio button in PHP?

查看:308
本文介绍了如何获得PHP中单选按钮的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个基本网站,要求用户选择一个单选按钮.我希望一个PHP文件检索选择的单选按钮的值并作出相应的响应,但是该文件当前不产生任何输出.我现在使用的代码有什么问题?为什么我的PHP文件无法正确检索单选按钮值?

I've created a basic website that requires the user to select a radio button. I want a PHP file to retrieve the value of the radio button that was chosen and respond accordingly, but the file does not currently produce any output. What is wrong with the code I am using now? Why can my PHP file not retrieve the radio button value properly?

Index.html:

Index.html:

<form method="POST">
    <input type="radio" name="MyRadio" value="First" checked>First<br> //This one is automatically checked when the user opens the page
    <input type="radio" name="MyRadio" value="Second">Second
</form>
<form method="GET" action="Result.php">
    <input type="submit" value="Result" name="Result"> //This button opens Result.php
</form>

Result.php:

Result.php:

<?php
$radioVal = $_POST["MyRadio"];

if($radioVal == "First")
{
    echo("You chose the first button. Good choice. :D");
}
else if ($radioVal == "Second")
{
    echo("Second, eh?");
}
?>

推荐答案

您正在为一般输入元素使用两种不同的形式,而一种形式仅由一个提交按钮组成.

Your are using two separate forms for your general input elements and one consisting of a submit button only.

在第一个表单中包括提交"按钮,它应该可以正常工作:

Include the submit button in the first form and it should work fine:

<form method="POST" action="Result.php">
    <input type="radio" name="MyRadio" value="First" checked>First<br> //This one is automatically checked when the user opens the page
    <input type="radio" name="MyRadio" value="Second">Second
    <input type="submit" value="Result" name="Result"> //This button opens Result.php
</form>

这篇关于如何获得PHP中单选按钮的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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