attaboy Posted February 5, 2012 Share Posted February 5, 2012 When I hit the submit button I would like the answer to appear in the box labled answer. Is that possible? <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Untitled Document</title> <style type="text/css"> input {position:absolute; left:120px;} p {margin-bottom:-10px;} </style> </head> <body> <form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>"> <p>first number:<input type="text" name="first_number" /></p> <p>second number:<input type="text" name="second_number" /></p> <p>answer:<input type="text" name="answer" /></p> <ul> <li>add: <input type="radio" name="group1" value="add" /></li> <li>subtract: <input type="radio" name="group1" value="subtract" /></li> <li>multiply: <input type="radio" name="group1" value="multiply" /></li> <li>divide: <input type="radio" name="group1" value="divide" /></li> </ul> <p><input type="submit" value="submit" /></p> </form> <?php $answer = $_POST['group1']; $first = $_POST['first_number']; $second = $_POST['second_number']; $ans=0; if ($answer == "add") { $ans = $first + $second; echo $ans; } else if ($answer == "subtract"){ $ans = $first - $second; echo $ans; } else if ($answer == "multiply"){ $ans = $first * $second; echo $ans; } else if ($answer == "divide"){ $ans = $first / $second; echo $ans; } else { echo 'damm...'; } ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/256472-how-do-i-get-display-answer-in-an-input-element/ Share on other sites More sharing options...
sunfighter Posted February 6, 2012 Share Posted February 6, 2012 Yes it is and a few more things also: file name is test_2.php <?php $ans=''; $what = 'answer:'; $group=''; $first=''; $second=''; if(isset($_POST["msoft"]) && ($_POST["msoft"] == "trash")) { if(isset($_POST['first_number'])) $first = $_POST['first_number']; if(isset($_POST['second_number'])) $second = $_POST['second_number']; if(isset($_POST['group1'])) $group = $_POST['group1']; if ($first != '' && $second != '') { switch ($group) { case "add": $ans = $first + $second; $what = 'addition'; break; case "subtract": $ans = $first - $second; $what = 'subtraction'; break; case "multiply": $ans = $first * $second; $what = 'multiplication'; break; case "divide": $ans = $first / $second; $what = 'division'; break; default: $ans = 'damm...'; } }else{ $ans = 'damm...'; } } ?> <!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" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> <style type="text/css"> input {position:absolute; left:120px;} p {margin-bottom:-10px;} </style> </head> <body> <form method="post" action="test_2.php"> <input type="hidden" name="msoft" value="trash" /> <p>first number:<input type="text" name="first_number" value="<?php echo $first; ?>" /></p> <p>second number:<input type="text" name="second_number" value="<?php echo $second; ?>" /></p> <p><?php echo $what; ?><input type="text" name="answer" id="answer" value="<?php echo $ans; ?>" /></p> <ul> <li>add: <input type="radio" name="group1" value="add" /></li> <li>subtract: <input type="radio" name="group1" value="subtract" /></li> <li>multiply: <input type="radio" name="group1" value="multiply" /></li> <li>divide: <input type="radio" name="group1" value="divide" /></li> </ul> <p><input type="submit" value="submit" /></p> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/256472-how-do-i-get-display-answer-in-an-input-element/#findComment-1315196 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.