Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New to the CF scene
    Join Date
    May 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Changing a value to % instead of +

    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.
    Code:
    <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

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,982
    Thanks
    203
    Thanked 2,536 Times in 2,514 Posts
    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

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    New to the CF scene
    Join Date
    May 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,982
    Thanks
    203
    Thanked 2,536 Times in 2,514 Posts
    Quote Originally Posted by Donna N View Post
    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.
    Last edited by Philip M; 07-04-2012 at 03:11 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #5
    New Coder
    Join Date
    Jun 2012
    Posts
    80
    Thanks
    0
    Thanked 18 Times in 18 Posts
    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.
    Code:
    <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.
    Code:
    <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.
    Code:
    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
    Last edited by Richter; 07-04-2012 at 09:14 PM.

  • #6
    New to the CF scene
    Join Date
    May 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Richter View Post
    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.
    Code:
    <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.
    Code:
    <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.
    Code:
    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

  • #7
    New Coder
    Join Date
    Jun 2012
    Posts
    80
    Thanks
    0
    Thanked 18 Times in 18 Posts
    Code part 3rd have to put it into "compute" function, the 3rd part of example code will replace entire code below.

    Code:
    //-------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.
    Code:
    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 + "]";
    Last edited by Richter; 07-05-2012 at 09:17 AM.

  • #8
    New to the CF scene
    Join Date
    May 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thnaks for the help
    Ok having a Blonde moment here
    Changed all the code and got $ NaN for the total

  • #9
    New Coder
    Join Date
    Jun 2012
    Posts
    80
    Thanks
    0
    Thanked 18 Times in 18 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •