...

View Full Version : Need help with my math equation getting values from form fields



Broll05
12-08-2010, 05:24 PM
I am having some trouble trying to fix my math equation to calculate the total rent by aquiring values from my form fields. Converting the date fields to days and finding the number of days via two date fields is mainly where I am having trouble. Also, I am trying to have a window pop up before submission but the onclick event does not seem to function properly. Any suggestions would be greatly appreciated. Thank you.

Below is the math equation I have come up with:


//calculate days from date field

function calcTotal(date1, date2) {

//assign variables

var equip = parseFloat(document.forms[0].equipment.value)
var pDate = parseFloat(document.forms[0].pickupDate.value)
var pHours = parseFloat(document.forms[0].pickupHours.value)
var pMinutes = parseFloat(document.forms[0].pickupMinutes.value)
var rDate = parseFloat(document.forms[0].returnDate.value)
var rHours = parseFloat(document.forms[0].returnHours.value)
var rMinutes = parseFloat(document.forms[0].returnMinutes.value)

// The number of milliseconds in one day
var ONE_DAY = 1000 * 60 * 60 * 24

// Convert both dates to milliseconds
var date1_ms = rDate.getTime()
var date2_ms = pDate.getTime()

// Calculate the difference in milliseconds

var difference_ms = Math.abs(date1_ms - date2_ms)


//calculate total cost and diplay in window.confirm() dialog box

var pTime = pHours + pMinutes;
var rTime = rHours + rMinutes;
var total = (((difference_ms/ONE_DAY) - 1) * 24) * equip + ((rTime + (24 - pTime)) * equip);
var OK = window.confirm(" The total rental cost is $" + total + "\n Click OK to accept, Cancel to decline");

if (OK) {return true}
else {return false}

}


Below is the body section of the form:


<input type = "submit" name = "submit" value = "Submit Reservation" onclick = "calcTotal()"/>

Philip M
12-08-2010, 05:36 PM
What exactly is the trouble you are having? We cannot guess that! Do you receive error messages? Or is the result of the calculation wrong?

What is the format of the dates as input by the user? Have you tried using your error console or Firebug? Or placing alerts in the code at strategic intervals to check the progress.

As before, you are trying to handle too many things at the same time. Get the pricing to work before you start thinking about pop-up windows.

Temporarily simplify you code to just calaculate the price based on days (not hours or minutes). Once that works then add other features gradually, testing at each stage.

onclick = "return calcTotal()"


Quizmaster: What R is a dish said to be best served cold?
Contestant: Toast.

Broll05
12-08-2010, 05:43 PM
I am getting an error that says that rDate.getTime is not a function, when clearly I have set it as a variable within a function.

How do I get the dates from the form field to function properly as the variables I am using and how can I incorporate those into my equation to get a total cost?

My test code below seems to work properly but when I try to assign the variables to form fields, I end up with the rDate.getTime is not a function as in my code in the above post.


<script>
//calculate days from date field

function calcTotal(date1, date2) {

//assign variables

var equip = 20
var pDate = new Date(2010,12,5)
var pHours = 9
var pMinutes = 0
var rDate = new Date(2010,12,8)
var rHours = 11
var rMinutes = 0

// The number of milliseconds in one day
var ONE_DAY = 1000 * 60 * 60 * 24

// Convert both dates to milliseconds
var date1_ms = rDate.getTime()
var date2_ms = pDate.getTime()

// Calculate the difference in milliseconds

var difference_ms = Math.abs(date1_ms - date2_ms)


//calculate total cost and diplay in window.confirm() dialog box

var pTime = pHours + pMinutes;
var rTime = rHours + rMinutes;
var total = (((difference_ms/ONE_DAY) - 1) * 24) * equip + ((rTime + (24 - pTime)) * equip);
var OK = window.confirm(" The total rental cost is $" + total + "\n Click OK to accept, Cancel to decline");

if (OK) {return true}
else {return false}

}
calcTotal("x","y")
</script>

some text to show on page

Philip M
12-08-2010, 07:23 PM
You do not show how the dates are being entered by the user. And you are not passing the date to rDdate in the correct format which must be YYYY, MM, DD.

To validate and find the difference between two dates entered in MM/DD/YYYY format, with hours and minutes obtained from elsewhere (drop-down select lists) :-


<form name = "myform">
Enter First Date MM/DD/YYYY <input type = "text" name = "date1" id = "date1"><br>
Enter Second Date MM/DD/YYYY <input type = "text" name = "date2" id = "date2"><br><br>
<br><br>

<input type = "button" value = "Check Date" onclick = "checkit()">
</form>

<script type = "text/javascript">

function checkit() {

var today = new Date();
var nowyear = today.getFullYear();

var d1 = document.myform.date1.value.split("/");
var yr = d1[2];
var mm = d1[0]-1; // months in Javascript are 0-11
var dy = d1[1];
var OK1 = checkValidDate(yr,mm,dy); // OK1 returns true or false
if ((yr < nowyear || yr > nowyear +1)) {OK1 = false} // only valid for current year and next year

var d2 = document.myform.date2.value.split("/");
yr = d2[2];
mm = d2[0]-1;
dy = d2[1];
var OK2 = checkValidDate(yr,mm,dy);
if ((yr < nowyear || yr > nowyear+2)) {OK2 = false}

if ((!OK1) || (!OK2)) {
alert ("Invalid dates\(s\) or incorrect format! Please try again.");
document.myform.date1.value = "";
document.myform.date2.value = "";
return false;
}

// arbitrary or example start and end times obtained from elsewhere (select lists) - make sure the values are numbers and not strings!

// EXAMPLE var fhrs = document.forms[0].pickupHours.value;
var fhrs = 11;
var fmins = 30;
var shrs = 14;
var smins = 0;

var firstDate = new Date(d1[2],d1[0]-1,d1[1],fhrs,fmins); // note month must be 0-11!! Also note USA date format
var secondDate = new Date(d2[2],d2[0]-1,d2[1],shrs,smins);
var difference = secondDate.getTime() - firstDate.getTime();
if (difference < 0) {
alert ("The second date must be after the first date!");
document.myform.date1.value = "";
document.myform.date2.value = "";
return false;
}

// convert milliseconds to days, hours, minutes

var secs = difference/1000;
var days = Math.floor(secs/86400);
secs %= 86400;
var hours= Math.floor(secs/3600);
if (hours<10) {hours = "0" + hours}
secs %= 3600;
var mins = Math.floor(secs/60);
if (mins<10) {mins = "0" + mins}
alert ("Time Difference is " + days + " Days " + hours + " Hours " + mins + " Minutes");
}


function checkValidDate(yr,mm,dy) {
var nd = new Date();
nd.setFullYear(yr,mm,dy); // YYYY,MM(0-11),DD
var ndmm = nd.getMonth();
if (ndmm != mm) {
return false;
}
else {
return true;
}
}

</script>

I do hope that this will put an end to your problems! :) The script calculates the days, hours, minutes between two dates/times. All you have to do is multiply the days by the daily rate and the hours by the hourly rate, then add em' together to get the total hire cost. Get that working properly before you try to move on to something else!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum