PDA

View Full Version : I Want to add to get total but rather script append as string ?



leonidassavvides
01-29-2009, 11:44 AM
var subprice, rather added to total in appended ...well ?
eg subprice=5 total=9 addition>59 rather 14

also the third script code below is it correct ?


<script type="text/javascript">
/* <![CDATA[*/
function setCookie(c_name,value)
{
document.cookie=c_name+ "=" +escape(value);
}
function getCookie(c_name)
{
if (document.cookie.length>0)
{
c_start=document.cookie.indexOf(c_name + "=");
if (c_start!=-1)
{
c_start=c_start + c_name.length+1;
c_end=document.cookie.indexOf(";",c_start);
if (c_end==-1) c_end=document.cookie.length;
return unescape(document.cookie.substring(c_start,c_end));
}
}
return "";
}
// below same for others + (march01.valueOf()==(pickup*1000))
if (getCookie('gps')!=null && getCookie('gps')!='') {
document.getElementById('gps').checked = getCookie('gps');
}
function calc() {

var group = document.extras.group.value;
var pickup = document.extras.pickup.value;
var march01 = new Date(2009,2,1);
var days_hd = document.extras.days_hd.value;
var subprice = document.extras.price.value;
var total = 0 ; // total = subprice ;

if (document.extras.drivers.value >= 1) {
total = total + (document.extras.drivers.value - 1)*days_hd*2;
}
if ((document.extras.cseat.value+document.extras.cbseat.value) >= 1) {
total += (document.extras.cseat.value+document.extras.cbseat.value-1)*days_hd*2;
}
if (document.extras.gps.checked == true) {
if (group=="E1") {
total += 0;
} else if (march01.valueOf()==(pickup*1000)) { // go to change this
total += 0;
} else {
total += 1*(days_hd/7)*10;
}
document.getElementById('gps1').value = "YES";
} else {
document.getElementById('gps1').value = "NO";
}
total += subprice; //
document.getElementById('t').innerHTML = "€" + total ;
setCookie('drivers',document.extras.drivers.value);
setCookie('cseat',document.extras.cseat.value);
setCookie('cbseat',document.extras.cbseat.value);
setCookie('gps',document.extras.gps.checked);
setCookie('transmissionchoosen',document.extras.transmissionchoosen.value);
document.getElementById('drivers1').value = document.getElementById('drivers').value;
document.getElementById('cseat1').value = document.getElementById('cseat').value;
document.getElementById('cbseat1').value = document.getElementById('cbseat').value;
// getCookie('gps')==true)?checked:unchecked;
// document.getElementById('gps1').value = document.getElementById('gps').value;
document.getElementById('transmissionchoosen1').value = document.getElementById('transmissionchoosen').value;
document.getElementById('finalprice').value = total;
// "(getCookie('gps')==true)?document.write('checked'):document.write('');"

}

/* ]]>*/
</script>


<table width="450" border="1" align="center" cellpadding="2" cellspacing="2" summary="Extras cost">
<caption>
EXTRAS
</caption>
<tr>
<td width="270" align="left" valign="middle">Extra drivers-1st FREE OF CHARGE (€2/day)</td>
<td width="160" align="left" valign="middle"><select name="drivers" id="drivers" onchange="calc();">
<option value="(getCookie('drivers')!=null && getCookie('drivers')!='') ? getCookie('drivers'):0;" selected="selected"><script type="text/javascript">(getCookie('drivers')!=null && getCookie('drivers')!='')?document.write(getCookie('drivers')):document.write("0");</script></option>
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select></td>
</tr>
<tr>
<td align="left" valign="middle">Small Seat-1st FREE OF CHARGE</td>
<td align="left" valign="middle">&nbsp;</td>
</tr>
<tr align="right" valign="middle">
<td align="left" valign="middle">Child Car Seat (€2/day)</td>
<td align="left" valign="middle"><select name="cseat" id="cseat" onchange="calc();" >
<option value="(getCookie('cseat')!=null && getCookie('cseat')!='') ? getCookie('cseat'):0;" selected="selected"><script type="text/javascript">(getCookie('cseat')!=null && getCookie('cseat')!='')?document.write(getCookie('cseat')):document.write("0");</script></option>
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select></td>
</tr>
<tr>
<td align="left" valign="middle">Child Booster Seat (€2/day)</td>
<td align="left" valign="middle"><label>
<select name="cbseat" id="cbseat" onchange="calc();" >
<option value="(getCookie('cbseat')!=null && getCookie('cbseat')!='') ? getCookie('cbseat'):0;" selected="selected"><script type="text/javascript">(getCookie('cbseat')!=null && getCookie('cbseat')!='')?document.write(getCookie('cbseat')):document.write("0");</script></option>
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
</label></td>
</tr>
<tr>
<td align="left" valign="middle">GPS (€10/week or FREE if special offer)</td>
<td align="left" valign="middle"><label>
<input name="gps" type="checkbox" id="gps" onclick="calc();" value="YES" />
</label></td>
</tr>
<tr>
<td align="left" valign="middle">Choose Transmission</td>
<td align="left" valign="middle"><label>
<select name="transmissionchoosen" id="transmissionchoosen">
<option value="(getCookie('transmissionchoosen')!=null && getCookie('transmissionchoosen')!='') ? document.write(getCookie('transmissionchoosen')):document.write('Automatic');" selected="selected"><script type="text/javascript">(getCookie('transmissionchoosen')!=null && getCookie('transmissionchoosen')!='')?document.write(getCookie('transmissionchoosen')):document.write("Automatic");</script></option>
<option value="Automatic">Automatic</option>
<option value="Manual">Manual</option>
<option value="Whatever">Whatever</option>
</select>
</label></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td align="right" valign="middle">TOTAL[Incl. extras]:</td>
<td bgcolor="#CCCCCC" id="t">&nbsp;</td>
</tr>
</table>

------------------------------------------------


<select name="transmissionchoosen" id="transmissionchoosen">
<option value="(getCookie('transmissionchoosen')!=null && getCookie('transmissionchoosen')!='') ? document.write(getCookie('transmissionchoosen')):document.write('Automatic');" selected="selected"><script type="text/javascript">(getCookie('transmissionchoosen')!=null && getCookie('transmissionchoosen')!='')?document.write(getCookie('transmissionchoosen')):document.write("Automatic");</script></option>
<option value="Automatic">Automatic</option>
<option value="Manual">Manual</option>
<option value="Whatever">Whatever</option>
</select>

jscheuer1
01-29-2009, 03:48 PM
The value of a form element is a string, not a number, so will be appended rather than added mathematically when the plus operator is used.

However, a string which represents a number may be converted to a number before addition is attempted. This may be done by giving it the (what I believe is called) unary plus sign, but that gets confusing when also using + as an operator, as they are the same character. An alternative is to subtract 0 from it.

There are ony two sections in the code where it looks like this needs to be done:



if ((document.extras.cseat.value+document.extras.cbseat.value) >= 1) {
total += (document.extras.cseat.value+document.extras.cbseat.value-1)*days_hd*2;


which should be:


if (((document.extras.cseat.value-0)+(document.extras.cbseat.value-0)) >= 1) {
total += ((document.extras.cseat.value-0)+(document.extras.cbseat.value-1))*days_hd*2;

and:


total += subprice; //

which should be:


total += (subprice-0); //

leonidassavvides
02-02-2009, 12:59 PM
I may use and parseInt() or parseFloat() ?

I want if onload[refresh page or submit to same page] to call function calc(); to calculate form fields...also to populate second form hidden fields , where to include ... window.onload=fromCookiesToSecondFormFields(); ?

The window.onload=calc(); May exist more than one in differ places ?

Hidden fields must created and after , to call assignment functions ? If yes, assignment functions come after hiddens ?

If we give values to non existed hiddens is error ? If we assign hiddens from js cookies then is better included in their form the assignment code in case there are two forms [their form is second] and second appears only after success submit to same page of first[here we have selection sequence - php] ?

review files
https://www.poliscarhire.com/customeradmin/booking-review24jan2009b.php
http://www.poliscarhire.com/js/booking-review-calc2SLfinal.js underconstruction

magicgavin
02-09-2009, 05:01 AM
Also using
-=- is the same as += but it will be treated as a number rather than a string.