Reputation: 57
I have a form using which i am going to calculate two numbers.
Right now, when i click the submit button, only then I get the answer. However, what I want is that whenever I put the values inside the boxes, then the answer should appear automaticallly.
Here is the code:
<!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>Calculate</title>
</head>
<body>
<form action="#" method="POST">
Number 1:
<input type="number" name="no1" />
Number 2:
<input type="number" name="no2" />
<input type="submit" value="Calculate" />
= <?php
@$no1 = $_POST['no1'];
@$no2 = $_POST['no2'];
echo $result = $no1 + $no2;
?>
</form>
</body>
</html>
Upvotes: 1
Views: 45
Reputation: 271
Try this
<form oninput="x.value=parseInt(a.value)+parseInt(b.value)">
<input type="number" id="a" value="50">+
<input type="number" id="b" value="50">=
<output name="x" for="a b"></output>
</form>
Upvotes: 3
Reputation: 9476
You need to study the difference between server-side scripting and client-side scripting, I think.
What you seem to want is for the user's browser to do the calculation work. In that case, you will write your code in javascript (I suggest looking up jQuery, which is a javascript framework). Then you can do exactly what you are asking.
Look up the onKeyPress
event for your inputs.
I searched "javascript onkeypress calculate" on google and saw some results that might help.
Upvotes: 0