...

View Full Version : Changing a value to % instead of +



Donna N
07-04-2012, 12:24 PM
Hi everyone :)
I am using this code and have tried to to figure a way off adding a say 10% or 20% to the total price via one of the drop down menus instead of a product.

<SCRIPT LANGUAGE="JavaScript">

var called=false;
var T_Price=0;
var Line1_flag;
var Line1_print="";
var Line2_flag;
var Line2_print;
var Line3_flag;
var Line3_print;
var Line4_flag;
var Line4_print;
var Line5_flag;
var Line5_print;
var Line6_flag;
var Line6_print;
function compute(form){
called=true;
//-------Productline 1----------
if (form.Productline1[0].selected){
Line1_print= "None [$0]";
Line1_flag=0;
}
if (form.Productline1[1].selected){
Line1_flag =84.95;
Line1_print="PlanMagic Business [$84.95]";
}
else if (form.Productline1[2].selected){
Line1_flag =99.95;
Line1_print="PlanMagic Marketing [$99.95]";
}
else if (form.Productline1[3].selected){
Line1_flag =144.95;
Line1_print="Finance Pro [$144.95]";
}
else if (form.Productline1[4].selected){
Line1_flag =199.95;
Line1_print="PlanMagic Hotel [$199.95]";
}
else if (form.Productline1[5].selected){
Line1_flag =139.95;
Line1_print="PlanMagic Restaurant [$139.95]";
}
else if (form.Productline1[6].selected){
Line1_flag =499.95;
Line1_print="All Software [$499.95]";
}
//-------Productline 2----------
if (form.Productline2[0].selected){
Line2_flag=0;
Line2_print="None [$0]";
}
if (form.Productline2[1].selected){
Line2_flag=150;
Line2_print="Product 1 [$150]";
}
if (form.Productline2[2].selected){
Line2_flag=200;
Line2_print="Product 2 [$200]";
}
if (form.Productline2[3].selected){
Line2_flag=300;
Line2_print="Product 3 [$300]";
}
//-------Productline 3-----------
if (form.Productline3[0].selected){
Line3_flag=0;
Line3_print="None [$0]";
}
if (form.Productline3[1].selected){
Line3_flag=10;
Line3_print="Product 1 [$10]";
}
if (form.Productline3[2].selected){
Line3_flag=20;
Line3_print="Product 2 [$20]";
}
if (form.Productline3[3].selected){
Line3_flag=30;
Line3_print="Product 3 [$30]";
}
//-------Productline 4-------
if (form.Productline4[0].selected){
Line4_flag=0;
Line4_print="None [$0]";
}
if (form.Productline4[1].selected){
Line4_flag=100;
Line4_print="Product 1 [$100]";
}
if (form.Productline4[2].selected){
Line4_flag=200;
Line4_print="Product 2 [$200]";
}
if (form.Productline4[3].selected){
Line4_flag=300;
Line4_print="Product 3 [$300]";
}
//-------Productline 5-------
if (form.Productline5[0].selected){
Line5_flag=0;
Line5_print="None [$0]";
}
if (form.Productline5[1].selected){
Line5_flag=50;
Line5_print="Product 1 [$50]";
}
if (form.Productline5[2].selected){
Line5_flag=60;
Line5_print="Product 2 [$60]";
}
if (form.Productline5[3].selected){
Line5_flag=70;
Line5_print="Product 3 [$70]";
}
//-------Productline 6-------
if (form.Productline6[0].selected){
Line6_flag=0;
Line6_print="None [$0]";
}
if (form.Productline6[1].selected){
Line6_flag=75;
Line6_print="Product 1 [$75]";
}
if (form.Productline6[2].selected){
Line6_flag=85;
Line6_print="Product 2 [$85]";
}
if (form.Productline6[3].selected){
Line6_flag=95;
Line6_print="Product 3 [$95]";
}
T_Price=Line1_flag+Line2_flag+Line3_flag+Line4_flag+Line5_flag+Line6_flag;
form.T_Price.value=" $ "+ T_Price;
}
function print(form){
if(!called){
compute(form);
}
text = ("<HEAD><TITLE>'Cost Esimator'</TITLE></HEAD>");
text = (text +"<BODY BGCOLOR = '#FFFFFF' ><CENTER><B><FONT SIZE = 3><FONT COLOR=BLUE>Cost Estimator</FONT></FONT></B>");
text= (text +"<br></CENTER>");
text=(text+"<hr>");
text=(text+"<TABLE BORDER =0><TR VALIGN=Top><TD VALIGN=Top>");
text=(text+"<B>Productline1: <BR>Productline2: <BR>Productline3: <BR>Productline4: <BR>Productline5: <BR>Productline6:");
text=(text+"</B></TD><TD>")
text=(text+"<B>"+ Line1_print+"<BR>"+ Line2_print+"<BR>"+ Line3_print+"<BR>");
text=(text+ Line4_print+"<BR>"+ Line5_print+"<BR>"+ Line6_print+"<BR>");
text=(text+"<TD></TR></TABLE><hr>");
text=(text+"<B><FONT COLOR=RED>Total Cost:</FONT>"+" $"+T_Price);
text=(text+"<BR><BR><FONT SIZE=-1><FONT COLOR=Gray>To print, choose File and Print.</FONT></FONT>");
text=(text+"</body></html>");
msgWindow=window.open("","displayWindow","toolbar=no,width=375,height=250,directories=no,status=yes,scrollbars=yes,resize=no,menubar=yes")
msgWindow.document.write(text)
msgWindow.document.close()
}
</SCRIPT>

<form method="post">
<table cellpadding="4" bgcolor="#EAE8E8">
<tr>
<td>Productline 1:<br>
<select name="Productline1">
<option selected>Select
<option>PlanMagic Business [$84.95]
<option>PlanMagic Marketing [$99.95]
<option>Finance Pro [$144.95]
<option>PlanMagic Hotel [$199.95]
<option>PlanMagic Restaurant [$139.95]
<option>All software [$499.95]
</select></td>
<td>Productline 2:<br>
<select name="Productline2">
<option selected>Select
<option>Product 1 [$150]
<option>Product 2 [$250]
<option>Product 3 [$350]
</select></td>
<td>Productline 3:<br>
<select name="Productline3">
<option>Select
<option>Product 1 [$10]
<option>Product 2 [$20]
<option>Product 3 [$30]
</select></td>
</tr>
<tr>
<td>Productline 4:<br>
<select name="Productline4">
<option>Select
<option>Product 1 [$100]
<option>Product 2 [$200]
<option>Product 3 [$300]
</select></td>
<td>Productline 5:<br>
<select name="Productline5">
<option>Select
<option>Product 1 [$50]
<option>Product 2 [$60]
<option>Product 3 [$70]
</select></td>
<td>Productline 6:<br>
<select name="Productline6">
<option>Select
<option>Product 1 [$75]
<option>Product 2 [$85]
<option>Product 3 [$95]
</select></td>
</tr>
<tr>
<td colspan="3">
<table cellpadding="2">
<tr>
<td><br>
<input type="BUTTON" name="Price" value="Update Price" onclick="compute(this.form)">
<input type="text" size="13" name="T_Price" value></td>
<td valign="top"><br>
<input type="BUTTON" name="Print_data" value="Print Preview" onclick="print(this.form)"></td>
</tr>
</table>
</td>
</tr>
</table>
</form>

Cant it even be done ? or will i have to find another script ?

Cheers Donna :)

Philip M
07-04-2012, 01:02 PM
Suggest you fix the errors in your markup before yuou fo any further. See http://validator.w3.org/

At a glance you have not closed any of your option tags. <script language = "Javascript"> is long obsolete.

I am not sure if I understand your question. Do you want to add the same percentage to all the products or a different percentage to different products?


It is your responsibility to die() if necessary….. - PHP Manual

Donna N
07-04-2012, 01:23 PM
I was trying to add a 10% and 20% option to one of the drop down menus to add to the over all price. :) instead off just adding a fixed number.

Philip M
07-04-2012, 03:04 PM
I was trying to add a 10% and 20% option to one of the drop down menus to add to the over all price. :) instead off just adding a fixed number.

Still do not undertand. Percentage of what? Fix your markup, then give a clear example of what you want to input via a dropdown and the result you hope to generate.

Richter
07-04-2012, 08:58 PM
Hi Donna N,
Do you want to make a new drop down list that has value as percent and when you change it, it will effect all price of another list when you sum all price, don't you ?

1. You need to put your price into value of option as example below.

<td>Productline 1:<br>
<select name="Productline1">
<option selected>Select</option>
<option value="84.95">PlanMagic Business [$84.95]</option>
<option value="99.95">PlanMagic Marketing [$99.95]</option>
<option value="144.95">Finance Pro [$144.95]</option>
<option value="199.95">PlanMagic Hotel [$199.95]</option>
<option value="139.95">PlanMagic Restaurant [$139.95]</option>
<option value="499.95">All software [$499.95]</option>
</select>

2. Add percents's drop down list into you form like this.

<td>Productline 1:<br>
<select name="AddCost">
<option selected>Select</option>
<option value="1.1">10%</option>
<option value="1.2">20%</option>
</select>

3. Get value and add your price as percent.

var TSS = form.Productline1;
var Price = +TSS.options[TSS.selectedIndex].value;

TSS = form.AddCost;
var T_Price = Price * +TSS.options[TSS.selectedIndex].value;

I only calculate Productline1 for show you how to get value from drop down list, the rest of this I think you can make it :)

Donna N
07-05-2012, 08:06 AM
Hi Donna N,
Do you want to make a new drop down list that has value as percent and when you change it, it will effect all price of another list when you sum all price, don't you ?

1. You need to put your price into value of option as example below.

<td>Productline 1:<br>
<select name="Productline1">
<option selected>Select</option>
<option value="84.95">PlanMagic Business [$84.95]</option>
<option value="99.95">PlanMagic Marketing [$99.95]</option>
<option value="144.95">Finance Pro [$144.95]</option>
<option value="199.95">PlanMagic Hotel [$199.95]</option>
<option value="139.95">PlanMagic Restaurant [$139.95]</option>
<option value="499.95">All software [$499.95]</option>
</select>

2. Add percents's drop down list into you form like this.

<td>Productline 1:<br>
<select name="AddCost">
<option selected>Select</option>
<option value="1.1">10%</option>
<option value="1.2">20%</option>
</select>

3. Get value and add your price as percent.

var TSS = form.Productline1;
var Price = +TSS.options[TSS.selectedIndex].value;

TSS = form.AddCost;
var T_Price = Price * +TSS.options[TSS.selectedIndex].value;

I only calculate Productline1 for show you how to get value from drop down list, the rest of this I think you can make it :)

Sounds exactly what I am after:), only problem I have is where to insert the 3rd part of the code....other 2 parts work great.

Also thanks Philip M for the markup link :)

Richter
07-05-2012, 09:13 AM
Code part 3rd have to put it into "compute" function, the 3rd part of example code will replace entire code below.


//-------Productline 1----------
if (form.Productline1[0].selected){
Line1_print= "None [$0]";
Line1_flag=0;
}
if (form.Productline1[1].selected){
Line1_flag =84.95;
Line1_print="PlanMagic Business [$84.95]";
}
else if (form.Productline1[2].selected){
Line1_flag =99.95;
Line1_print="PlanMagic Marketing [$99.95]";
}
else if (form.Productline1[3].selected){
Line1_flag =144.95;
Line1_print="Finance Pro [$144.95]";
}
else if (form.Productline1[4].selected){
Line1_flag =199.95;
Line1_print="PlanMagic Hotel [$199.95]";
}
else if (form.Productline1[5].selected){
Line1_flag =139.95;
Line1_print="PlanMagic Restaurant [$139.95]";
}
else if (form.Productline1[6].selected){
Line1_flag =499.95;
Line1_print="All Software [$499.95]";
}

I recode the 3rd part, this should help you understand how to use it better then old one.

function GetOption(SelectElement) { Return SelectElement.options[SelectElement.selectedIndex]; }
var TOpt = GetOption(form.Productline1);

Line1_flag = +TOpt.value * +GetOption(form.AddCost).value;
Line1_print = TOpt.text.slice(0, TOpt.text.indexOf("[")) + "[$" + Line1_flag + "]";

Donna N
07-05-2012, 10:01 AM
Thnaks for the help :)
Ok having a Blonde moment here :p
Changed all the code and got $ NaN for the total :(

Richter
07-05-2012, 12:02 PM
Did you change all "Productline" like 1st part of example code ?
I just code only Productline1 for example, you need to do it with the rest too.
Put each value into each product in product line.

"NaN" will return if you can't get value as number, check in your flag variant it must be some of them has NaN value and it make your total price return NaN in the end.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum