Page 1 of 1

Getting a form field to change when another field changes

Posted: Mon Jun 03, 2002 3:54 pm
by tarron
I have three fields, a start time, finish time, and total time. I've worked out the code to do the calculation of the difference, but I don't know how to make the form automatically enter the difference into the total time field. For example, if a use puts 7:00 into the start time, and 8:00 into the finish time, I want 1 to automatically pop up in the third field (totaltime)

My formula is as follows, but I don't know how to make the actual php form auto-update like that. This is probably a total newbie question, but that's what I am.

Thanks


$starttime = strtotime("$start_time_field");
$finishtime = strtotime("$finish_time_field");
$total_time_field = ((($finishtime - $starttime)/60)/60);

Posted: Mon Jun 03, 2002 4:01 pm
by twigletmac
The quick answer:

The form will have to be submitted for PHP to work its magic. It's server side technology so the information has to be sent back to the server for calculations to be done.

If you want stuff to automatically popup without users having to click a submit button you'll have to use Javascript because it can do things client side.

Mac

Posted: Mon Jun 03, 2002 4:05 pm
by tarron
Yeah, I figured that would be the answer. =/

Posted: Mon Jun 03, 2002 4:48 pm
by tarron
Ok, then any suggestions on how I go about this? I am trying to make a form where people enter times into a mysql database. They enter their start time, their finish time, and a total time worked. The kicker is that the total time has to be a real number, hence the formula I posted above.

How should I go about doing this? I have my form and database working, I just can't get the totaltime aspecet worked out.

Posted: Mon Jun 03, 2002 4:49 pm
by jason
Could you just take the start time, finish time, and calculate the total time?

Posted: Mon Jun 03, 2002 5:00 pm
by tarron
Ok, the way I would like the form to work is to have the user enter their name, start time, finish time and total time. When they click the submit button, those four items of information go into a database.

The problem I face is that the total time has to be a real number, not a time, so I used this formula to fix that

$starttime = strtotime("$start_time");
$finishtime = strtotime("$finish_time");
$totaltime = ((($finishtime - $starttime)/60)/60);

the problem I face is making that $totaltime result automatically go into the database. I don't want the user to make the calculation, I want them to just have to enter their start and finish times, yet the start, finish, and total times end up in the database. Along with any other info I have in the form.

Posted: Mon Jun 03, 2002 5:07 pm
by twigletmac
On the script that runs when you click the submit button all you need to do is those calculations and then use an insert query to put both the user inputted data (start time and finish time) and your calculated data (totaltime) into the database.

Mac

Posted: Tue Jun 04, 2002 6:10 am
by mikeq
As a rule of thumb in database design you don't include fields that can be calculated from data already there, just calculate it when you want to output it.

Exceptions are data warehouses when you want improve the performance of reporting.

Mike

Posted: Tue Jun 04, 2002 6:20 am
by twigletmac
Good point mikeq, you know you're tired when you just answer the question without actually thinking about what's being asked. At least that's my excuse.

Mac

Posted: Tue Jun 04, 2002 11:35 am
by tarron
Thanks for the good advice everyone.