在下面的代码中,我没有检查任何单选按钮,也没有将文本框保持空白并单击“提交”按钮。 我希望用户只看到最后的其他声明,即; “在文本框中输入一些值”。
当文本框为空白但选择了单选按钮时,代码可以正常工作。
请告知我应该在代码中进行哪些更改,以便在文本框为空并且未选中单选按钮时输出“在文本框中输入一些值”作为输出。
This is how my current output looks
<html>
<body>
<form action = "Convertor.php" method = "post">
Enter Number <input type = "text" name = "n1" placeholder = "Enter only
number"><br><br>
<input type = "radio" name = "r" value = "rs"> Convert USD to Rs <br>
<input type = "radio" name = "r" value = "m"> Convert Meter To
Centimeter <br>
<input type = "radio" name = "r" value = "d"> Convert Degree to
Fahrenheit <br>
<input type="submit" value="Submit" name = "Submit">
</form>
</body>
</html>
<?php
if(isset($_POST['n1']))
{
$no1 = $_POST['n1'];
$result = 0;
$condition = $_POST['r'];
if($no1 != "" || $condition == "") //Also tried !isset($_POST['r']) in
place of $condition == ""
{
if(is_numeric($no1))
{
switch($condition)
{
case "rs":
$result = $no1 * 65;
break;
case "m":
$result = $no1 * 100;
break;
case "d":
$result = $no1 * 1.8 + 32;
break;
}
echo $result;
}
else
{
echo "Enter only numbers";
}
}
else
{
echo "Enter some value in textbox";
}
}
?>
答案 0 :(得分:0)
您可以使用isset函数检查是否选择了无线电广播
<?php
if (isset($_POST['n1'])) {
$no1 = $_POST['n1'];
$result = 0;
$condition = isset($_POST['r']) ? $_POST['r'] : "";
if(!$condition) {
echo "Please select the Conversion Type";
} else {
if ($no1) //Also tried !isset($_POST['r']) in
{
if (is_numeric($no1)) {
switch ($condition) {
case "rs":
$result = $no1 * 65;
break;
case "m":
$result = $no1 * 100;
break;
case "d":
$result = $no1 * 1.8 + 32;
break;
}
echo $result;
} else {
echo "Enter only numbers";
}
} else {
echo "Enter some value in textbox";
}
}
}
?>
如果你愿意,你可以使用必需的属性来确保用户输入值(或检查收音机),你也可以为输入编号而不是文本类型
检查一下:
<input type="number" name="n1" placeholder="Enter only number" required="required"><br><br>
<input type="radio" name="r" value="rs" required="required"> Convert USD to Rs <br>
<input type="radio" name="r" value="m" required="required"> Convert Meter To Centimeter <br>
<input type="radio" name="r" value="d" required="required"> Convert Degree to