...

View Full Version : No Response from my Script - Am I missing Something



TimmyTimbo
09-26-2005, 05:00 PM
Hi There,

Hope you can help.

I have a mini form, for which I am trying to calculate a price based on 3 variables. I thought it was correct, but I dont get a response.
Please let me know if you can help with this?

JAVASCRIPT:

<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
function getPriceForDays() {
if (weeks == 0, days == 1) return (price / 0.5);
if (weeks == 0, days == 2) return (price / 0.7);
if (weeks == 0, days == 1) return (price / 0.9);
if (weeks >= 1, days == 3) return (price / 0.2);
if (weeks >= 1, days == 2) return (price / 0.4);
if (weeks >= 1, days == 1) return (price / 0.2);
}
function getPriceForWeeks() {
if (weeks >= 0) return (price * weeks)
}
function getNumberOfWeeks() {
var weeks = document.calculator.weeks.value;
return (weeks == "") ? 0 : weeks;
}
function getNumberOfDays() {
var days = document.calculator.days.value;
return (days == "") ? 0 : days;
}
function getPrice() {
var price = document.calculator.price.value;
return (price == "") ? 0 : price;
}
function showResult(result) {
document.calculator.response.value = result;
}
fuction formatPrice(value) {
var result = (days * weeks);
return result;

// End -->
</Script>

FORM:

<form id="form1" name="calculator">
<table width="44%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="22%">Days</td>
<td width="78%"><label>
<input name="days" type="text" id="days" />
</label></td>
</tr>
<tr>
<td>Weeks</td>
<td><input type="text" name="weeks" /></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><p>
<input type="hidden" name="price" value="100"/>
</p>
<p>
<label>
<input type="submit" name="Submit" value="Submit" onClick="formatPrice(value)"/>
</label>
</p></td>
</tr>
<tr>
<td>Total Price </td>
<td><label>
<input type="text" name="response" />
</label></td>
</tr>
</table>
<p>&nbsp;</p>
</form>
</body>


Basically, the code is for a rental, based on a number of days and weeks. The price is a hidden field. The problem i have is that the cost per day changes once the value of weeks is greater that 0.

I am new to this but have a good knowledge of HTML, and a growing knowldge of PHP, and Perl.

Cheers
Tim

Philip M
09-26-2005, 05:58 PM
if (weeks == 0, days == 1) return (price / 0.5);
if (weeks == 0, days == 2) return (price / 0.7);
if (weeks == 0, days == 1) return (price / 0.9);

The syntax is incorrect and weeks/0 days/1 is repeated.

Try:-

if (weeks == 0 && days == 1) return (price / 0.5);
if (weeks == 0 && days == 2) return (price / 0.7);
if (weeks == 0 && days == 3) return (price / 0.9);

Lerura
09-26-2005, 06:33 PM
your javascript is totally messed up

1.
a if is constructed this way:
if (condition){statement}

2.
you must use "&&" instead of ","
if (weeks == 0 && days == 1){}

3.
A function cannot run unless you call it.
you are trying to use then as automatic subrutines.

Beside that:

My suggestion is that you skip the weeks so that when you're eg. want a 10 day rental you enter "10 days" instead of "1 weeks and 3 days"

if i'm right you have 2 different rent's per day. a high for the first 7 days and a
low for the rest of the days.

If so, you can use this:


<script><!--
HighRent=100;
LowRent=50;

function formatPrice(){

Days = document.calculator.days.value;
if (Days == ""){Days = 0}
Days = eval(Days);

if (Days > 7){
HighDays = 7;
LowDays = Days - 7;
}else{
HighDays = Days;
LowDays = 0;
}

TotalRental = HighDays * HighRent + LowDays * LowRent;
return TotalRental;
}
// --></script>

if you are submitting it the right way i dont know



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum