automatically executes calculation

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
myplaces
Forum Newbie
Posts: 11
Joined: Wed Nov 12, 2008 3:25 pm

automatically executes calculation

Post 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...
User avatar
Peter Anselmo
Forum Commoner
Posts: 58
Joined: Wed Feb 27, 2008 7:22 pm

Re: automatically executes calculation

Post 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.
myplaces
Forum Newbie
Posts: 11
Joined: Wed Nov 12, 2008 3:25 pm

Re: automatically executes calculation

Post 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
Post Reply