Page 1 of 1

automatically executes calculation

Posted: Mon Dec 01, 2008 2:56 pm
by myplaces
how to execute logical function without clicking button.
For example html form, where visitor can enter two numbers. Then click button - calculate and php script does calculation
Html code something like this
<form method = "POST" action = "'.$_SERVER['PHP_SELF'].'">
<input type="text" name="first_number" size="5">
<input type="text" name="second_number" size="5">
<input type="submit" name="doit" value="calculate">
</form>
then php script to calculate, for example $first_number+$second_number

But how to do calculation automaticaly? without this <input type="submit" name="doit" value="calculate">
Visitor enters numbers and calculation is shown automatically, depending entered numbers...

Re: automatically executes calculation

Posted: Mon Dec 01, 2008 3:02 pm
by Peter Anselmo
To be done automatically, you must use Javascript, not PHP. PHP can only be used when the web server is determining what to send to the client. You want this action to be performed client-side, which requires Javascript.

You can do it using the "onchange" handler in the form fields

Example:

Code: Select all

<input type="text" name="first_number" onchange="AddNumbers();" />
You would of course need to define the "AddNumbers()" function to, in fact, add the numbers and display their value somewhere.

Re: automatically executes calculation

Posted: Mon Dec 01, 2008 3:12 pm
by myplaces
Thanks for answer. If the only way is javascript, it is not so good. because visitor may disabled javascript....Ok. then better php with button