I am new to php, just made a simple calculator. when I don't select the radio buttons; a notice come up.
Notice: Undefined index: r in E:\MY LOCAL SERVER\htdocs\php practise\calculator-form-advanced.php on line 59
Also I want show results in this input section
<p> <label for="result">Result</label> <input type="text" value="" /> </p>
---
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <form id="form1" name="form1" method="post" action=""> <p> <label for="num1">Number 1</label> <input type="text" name="num1" id="num1" /> </p> <p> <label for="num2">Number 2</label> <input type="text" name="num2" id="num2" /> </p> <p> <input type="radio" name="r" id="add" value="add" /> <label for="add">Addition</label> </p> <p> <input type="radio" name="r" id="sub" value="sub" /> <label for="sub">Subtraction</label> </p> <p> <input type="radio" name="r" id="mul" value="mul" /> <label for="mul">Multiplication</label> </p> <p> <input type="radio" name="r" id="div" value="div" /> <label for="div">Division</label> </p> <p> <input type="submit" name="submit" id="submit" value="Submit" /> </p> <p> <label for="result">Result</label> <input type="text" value="" /> </p> </form> <?php if (isset($_POST["submit"])) { $num1 = $_POST['num1']; $num2 = $_POST['num2']; $action = $_POST['r']; if ( $num1==''|| $num2=='' || $action=='' ) echo 'Something missing'; else { switch ($action) { case "add": { $result = $num1 + $num2; echo $result; break; } case "sub": { $result = $num1 - $num2; echo $result; break; }case "mul": { $result = $num1 * $num2; echo $result; break; }case "div": { $result = $num1 / $num2; echo $result; break; } } } } ?> </body> </html>
try checking if the radio button has been selected before assigning it to a variable