PHP 和 HTML 单选按钮错误


PHP and HTML radio button errors

我是PHP的新手,最近学会了如何用PHP编程,并且已经对HTML有所了解。我一直在使用单选按钮、POST 表单以及内置的 PHP 函数(包括 decbin、dechex 等)处理此代码,请检查代码中的错误。我相信这是我自己的错误之一,但帮助会得到尊重。索引

.HTML
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
<title>Radio Buttons</title>
</head>
<body>
<script>
function validateForm() 
{
var x=document.forms["values"]["numeral"].value;
if (x==null || x=="")
{
alert(":(");
return false;
}
}
</script>
<link href='http://fonts.googleapis.com/css?family=Open+Sans:400,300,700'         rel='stylesheet' type='text/css'>
<link href='http://fonts.googleapis.com/css?family=Sofia' rel='stylesheet' type='text/css'>
<div class = 'login'>
<h2>Number Converter</h2>
<form method="post" action="conversion.php" name='values'>
<input name='numeral' placeholder='A Number' type='text' autocomplete="off">
<input type="radio" name="rate" id="r1" value="Decimal to others" checked hidden>
<label for="r1" class="radio"><span></span>Decimal to Others</label>
<input type="radio" name="rate" id="r2" value="Binary to others" hidden>
<label for="r2" class="radio"><span></span>Binary to Others</label>
<input type="radio" name="rate" id="r3" value="HexaDecimal to others" hidden>
<label for="r3" class="radio"><span></span>HexaDecimal to others</label>
<input class='animated' type='submit' value='Convert' name='convert'> 
<a class='forgot' href='http://www.facebook.com/nalinbhardwaj.nib' target='_blank'>This project was created by Nalin Bhardwaj</a>
</form>
</div>
</body>
</html>

这是转换.php

转换。.PHP

<?php
$a = $_POST["numeral"];
if (isset($_POST['convert'])) {
$selected_radio = $_POST['rate'];
if ($selected_radio == 'Decimal to Others') {
echo decbin($a);
echo dechex($a);
}
}
?>

本节中有一些语法错误。

$a = $_POST["numeral"]  // semicolon missing
if ($selected_radio = = 'Decimal to Others') {   // unwanted space
echo decbin($a)     // semicolon missing
echo dechex($a)     // semicolon missing
}

将此部分更改为:

    $a = $_POST["numeral"];
   if (isset($_POST['convert'])) {
   $selected_radio = $_POST['rate'];
   if ($selected_radio == 'Decimal to Others') {
   echo decbin($a);
   echo dechex($a);
}
}

我认为在值检查中"十进制到其他人"是"十进制到其他人"o在您的无线电值中是小写的

 <?php
$a = $_POST["numeral"]
if (isset($_POST['convert'])) {
$selected_radio = $_POST['rate'];
if ($selected_radio == 'Decimal to Others') {
echo decbin($a);
echo dechex($a);
}
}

?>

据我所知,如果您想将单选按钮值提交到另一个文件中,请尝试添加额外的隐藏数据类型以及单选按钮(因为单选按钮值有时不会提交)

    <form method="post" action="conversion.php" name='values'>
    <input type="hidden" name="radio_value" id="radio_value" value="" />
     <input name='numeral' placeholder='A Number' type='text' autocomplete="off">
     <input type="radio" name="rate" id="r1" onClick="document.getElementById(radio_value).value='Decimal to others'" >
     <label for="r1" class="radio"><span></span>Decimal to Others</label>
     <input type="radio" name="rate" id="r2" onClick="document.getElementById(radio_value).value='Binary to others'" >         
     <label for="r2" class="radio"><span></span>Binary to Others</label>
     <input type="radio" name="rate" id="r3" onClick="document.getElementById(radio_value).value='HexaDecimal to others'" >         
     <label for="r3" class="radio"><span></span>HexaDecimal to others</label>
     <input class='animated' type='submit' value='Convert' name='convert'> 
     <a class='forgot' href='http://www.facebook.com/nalinbhardwaj.nib' target='_blank'>This project was created by Nalin Bhardwaj</a>
    </form>

在Javascript中改变这个

       $a = $_POST["numeral"];
     if (isset($_POST['convert'])) {
       $selected_radio = $_POST['radio_value'];
     if ($selected_radio == 'Decimal to others') {
       echo decbin($a);
       echo dechex($a);
      }