Getting a form field to change when another field changes

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
tarron
Forum Newbie
Posts: 13
Joined: Thu May 23, 2002 12:09 pm

Getting a form field to change when another field changes

Post 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);
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
tarron
Forum Newbie
Posts: 13
Joined: Thu May 23, 2002 12:09 pm

Post by tarron »

Yeah, I figured that would be the answer. =/
tarron
Forum Newbie
Posts: 13
Joined: Thu May 23, 2002 12:09 pm

Post 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.
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post by jason »

Could you just take the start time, finish time, and calculate the total time?
tarron
Forum Newbie
Posts: 13
Joined: Thu May 23, 2002 12:09 pm

Post 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.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
User avatar
mikeq
Forum Regular
Posts: 512
Joined: Fri May 03, 2002 3:33 am
Location: Edinburgh, Scotland

Post 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
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
tarron
Forum Newbie
Posts: 13
Joined: Thu May 23, 2002 12:09 pm

Post by tarron »

Thanks for the good advice everyone.
Post Reply