Krishna Krishna - 3 years ago 164
HTML Question

Getting error message when is radio button not checked

In below code I am not checking any radio button as well as keeping the textbox blank and clicking on submit button.
I want the user to see only the final else statement i.e.; "Enter some value in textbox".

The code works fine when the textbox is blank but radio button is selected.

Kindly advise what changes should I make in the code to get the "Enter some value in textbox" as the output when textbox is blank and radio button is not selected.

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";
}
}
?>

Answer Source

you can use isset function to check if the radio is selected bro

<?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";
        }
    }
}
?>

And if you want to, you can use required atrribute to make sure user have enter the value (or check the radio), and you can also you number type instead of text type for the input

check this out:

        <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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download