如何在PHP中获取单选按钮的值?
我创建了一个基本网站,要求用户选择一个单选按钮。我想要一个PHP文件来检索所选单选按钮的值并做出相应的响应,但该文件当前不生成任何输出。我现在使用的代码有什么问题?为什么我的PHP文件不能正确检索单选按钮值 Index.html:如何在PHP中获取单选按钮的值?,php,html,Php,Html,我创建了一个基本网站,要求用户选择一个单选按钮。我想要一个PHP文件来检索所选单选按钮的值并做出相应的响应,但该文件当前不生成任何输出。我现在使用的代码有什么问题?为什么我的PHP文件不能正确检索单选按钮值 Index.html: <form method="POST"> <input type="radio" name="MyRadio" value="First" checked>First<br> //This one is automatica
<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?");
}
?>
您的通用输入元素使用两个单独的表单,其中一个表单仅包含提交按钮 在第一个表单中包括submit按钮,它应该可以正常工作:
<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>
首先
//当用户打开页面时,会自动选中此选项
第二
//此按钮打开Result.php
这是正在提交的表单。它有一个action属性,该属性将其指向Result.php
<form method="GET" action="Result.php">
<input type="submit" value="Result" name="Result"> //This button opens Result.php
</form>
首先,你做错了一点。您正在使用两个表单来完成此任务。让我告诉你怎么做 index.html
<form action= "result.php" 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
<br/>
<input type="submit" value="Result" name="Result"> <!--//This button opens Result.php -->
首先
第二
result.php
<?php
echo $_POST["MyRadio];
// on new page you will get "First" or "Second", depending on what you have selected on html page
?>
您正在为html代码使用两个单独的表单,这意味着当您按下按钮时,第一个表单实际上没有提交
您不需要更改result.PHP
中的PHP代码,但最好使用一种表单
<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
<input type="submit" value="Result" name="Result"> //This button opens Result.php
</form>
首先
//当用户打开页面时,会自动选中此选项
第二
//此按钮打开Result.php
<?php
if (isset($_POST['Result']))
{
$radioVal = $_POST["MyRadio"];
if($radioVal == "First")
{
echo("You chose the first button. Good choice. :D");
}
else if ($radioVal == "Second")
{
echo("Second, eh?");
}
}
?>
<form action= "result.php" 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
<br/>
<input type="submit" value="Result" name="Result"> <!--//This button opens Result.php -->
<?php
echo $_POST["MyRadio];
// on new page you will get "First" or "Second", depending on what you have selected on html page
?>
<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
<input type="submit" value="Result" name="Result"> //This button opens Result.php
</form>