Jump to content

how do I get display answer in an input element?


attaboy

Recommended Posts

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>

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.