Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Trying to write a "quote builder" in JavaScript and HTML for a business webisite

Featured Replies

My aim is to create a form whereby the user can input the number of a services required (in a text input box that validates numerically), and where I can specify the price. The catch is that I need to be able to specify how the price adds up i.e if the user enters "1" the quote generator needs to output "£50" however if the user inputs "2", the quote generator needs to output "£99". The further catch is that it needs to be possible for the user to input any number up till 100 before a pop-up advises to email for a quote.

 

The closest thing I've been able to find from searching forums and google has been this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Calc</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"></script>
<script type="text/javascript">
<!-- 
$(document).ready(function() {
	$('#calculateTotal').click(function() {
		
			var shirtsPrice = 10.50, perColorPrice = 5.99;	
			
			var inputNum_of_Shirts = $('#shirts').val();
			var inputNum_of_FColor = $('#colorsOnFront').val();
			var inputNum_of_BColor = $('#colorsOnBack').val();
			
			var totalCost = (inputNum_of_Shirts*parseFloat(shirtsPrice))
			+(parseFloat(perColorPrice)*inputNum_of_FColor)
			+(parseFloat(perColorPrice)* inputNum_of_BColor );
			var perShirtCost = (parseFloat(shirtsPrice))
			+(parseFloat(perColorPrice)*inputNum_of_FColor)
			+(parseFloat(perColorPrice)*inputNum_of_BColor);
			
			$('#total').html(formatCurrency(totalCost));
			$('#perShirt').html(formatCurrency(perShirtCost));
			$('#result').css('display', 'block');
	});
});
// This Function I have searched from Web 
function formatCurrency(strValue)
{
	strValue = strValue.toString().replace(/\$|\,/g,'');
	dblValue = parseFloat(strValue);

	blnSign = (dblValue == (dblValue = Math.abs(dblValue)));
	dblValue = Math.floor(dblValue*100+0.50000000001);
	intCents = dblValue%100;
	strCents = intCents.toString();
	dblValue = Math.floor(dblValue/100).toString();
	if(intCents<10)
		strCents = "0" + strCents;
	for (var i = 0; i < Math.floor((dblValue.length-(1+i))/3); i++)
		dblValue = dblValue.substring(0,dblValue.length-(4*i+3))+','+
		dblValue.substring(dblValue.length-(4*i+3));
	return (((blnSign)?'':'-') + '$' + dblValue + '.' + strCents);
}

-->
</script>
</head>
<body>
<form id="calc" name="calc">
  	<p>
      <input id="shirts" name="shirts" type="text">
      <label for="shirts">Number of Shirts</label>
    </p>
    <p>
      <input id="colorsOnFront" name="colorsOnFront" type="text">
      <label for="colorsOnFront">Number of Colors on Front</label>
    </p>
    <p>
      <input id="colorsOnBack" name="colorsOnBack" type="text">
      <label for="colorsOnBack">Number of Colors on Back</label>
    </p>
    <p><input name="calculateTotal" id="calculateTotal" type="button" value="Calculate Total" /></p>
    <div id="result" style="display:none;"><strong>Total:</strong> <span id="total"></span> <strong>Per Shirt:</strong> <span id="perShirt"></span></div>
  </div>
</form>
</body>
</html>

The only thing wrong with this is that there can only be one set price, I need 100 different prices that can be called up depending on what number under 100 the user inputs.

IS THIS EVEN POSSIBLE?

I would be so grateful if someone could shed some light!

Cheers,
Duan

 

Hey Duan,

 

Is there a formula for achieving the shirtsPrice, or is this something you do manually? If you had a formula of sorts, you could use that to generate the shirtsPrice and ultimately get your total price based on that.

  • Author

So mine would go some thing like:

 

user inputs 1 - the price is £50

user inputs 2 - (50 x 2) -1% = £99

user inputs 3 - (50 x 3) -2% = £147

user inputs 4 - (50 x 4) -3% = £194

etc..

 

Would something like that work?

I've gotten this far (sorry, my Maths isn't the best :p). Hope you can work from it :)

 

I'll be back tomorrow if you need more help.

 

p.s. If you need me to explain the code, just give me a shout :)

 

http://jsfiddle.net/LyndseyB/97RBC/2/

Edited by Lyndsey
Updated version

  • Author

Hey it's actually possible this is fantastic!! Thanks so much Lyndsey!!

 

I'm too thick to get this into Dreamweaver but once I do I plan to add an extra £25 check box. Hopefully I'll be able to go from here but I'll post here if not!

Thanks again!

No worries, update me if you need more help. Don't forget to include the jQuery library in the <head> section before the function is run as it has been created using the jQuery library. A quick google search for jQuery will help you out (on mobile at the moment, sorry) :)

  • Author

Let me start by thanking you guys, I really didn't expect such helpful people! I have some more questions but I do realise that people get paid to do this stuff. So seriously, just tell me when to stop bugging you guys for help and to start paying someone (and then your last piece advice can be who to pay!).

So anyway I got the code that Lyndsey made into Dreamweaver and made some changes to the html to make it suit my needs a little better. I wasn't sure how to adjust the script however. For example, I deleted some text boxes so now surely I have some redundant javascript?

Here's my code as it is at the moment:

 

<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<title>Untitled Document</title>
</head>

<body>

<form id="calc" name="calc">
<p>
<H1>Quote Builder</H1>
</p>
<p>Please input the number of songs you wish to submit for mixing. </p>
<p>
      <input id="mixes" name="mixes" type="text" />
      <label for="mixes">Number of mixes</label>
</p>
<p>Optional Extras (£25 each):</p>
<p>
  <input type="checkbox" name="extras" value="25" class="chk-option" />  <label for "mixes">24bit Stems (ready for mastering)</label>
</p>
<p>
  <input type="checkbox" name="extras" value="25" class="chk-option" />  <label for "mixes">A Capella Mix</label>
</p>
<p>
  <input type="checkbox" name="extras" value="25" class="chk-option" />  <label for "mixes">Instrumental Mix</label>
</p>
<p>
    <input name="calculateTotal" id="calculateTotal" type="button" value="Calculate Total" />          
</p>
    <div id="result" style="clear:both;">
        <strong>Total:</strong> 
        <span id="total"></span> 
        <strong>Per mix:</strong> 
        <span id="permix"></span>
   </div>
  
</form>
<script>
$(document).ready(function() {
	$('#calculateTotal').click(function() {
            //individual mix price before reduction
            var mixPriceIndiv = 50;
            //number of mixes selected by user
            var nummixes = parseInt($('#mixes').val());
            //get mix price based on number of mixes 
            var mixPrice = getmixPrice(nummixes);    
            //per color price
			var perColorPrice = 5.99;
            //num front colors
            var frontColors = ($('#colorsOnFront').val()) ?parseInt($('#colorsOnFront').val()) : 0;
            //num back colors
            var backColors = ($('#colorsOnBack').val()) ? parseInt($('#colorsOnBack').val()) : 0;
			
            console.log(mixPrice);
            var totalCost = mixPrice + (perColorPrice*frontColors) + (perColorPrice*backColors);
        
            //grand total
            $('#total').html('£' + totalCost.toFixed(2));
        
            var permixCost = mixPriceIndiv + (perColorPrice*frontColors) + (perColorPrice*backColors);
            //per mix
            $('#permix').html('£' + permixCost.toFixed(2));
			
	});
    
    function getmixPrice(el) {
        numS = parseInt(el);        
        //calculate mix price
        shPrice = 50*numS;        
        //get percentage value based on number of mixes e.g. if 2 mixes, take off 1%
        percentVal = (el!==1) ? ((numS-1)/100)*shPrice : 1                      
        //get mix price minus the percentage
        mixPrice = (el!==1) ? (50*el) - percentVal : 50   
        console.log(mixPrice);
        return mixPrice;
    }
});
  </script>

</body>
</html>

Question 1. What do I add the code to get the calculator to include the check-boxes?
Question 2. Once a user has used the calculator to get an idea of how much this service will cost them, how do I move on with the information they have provided?

In my head I see it working like this... - They use the calculator and then click a link e.g "agree", and are then lead to page where they can upload their un-mixed material and input their bank details. How do I get the new page to know how many songs this user chose to upload for mixing, in the last page?

Question 3. Can anyone suggest a way of getting around the situation where someone wants to upload 4 tracks to be mixed, but only wants an 'optional extra' on 1 of them?

I hope this isn't too much to ask guys, let me know if it is!

Duan

Hey Duan,

 

Question 1: Here is an updated version using the checkboxes. http://jsfiddle.net/XwJrJ/. What I added in, was this:

 

$('.chk-option').each(function() {  
    if( $(this).is(':checked') ) {
       mixPrice+= parseInt($(this).val());              
    }
});

It checks for any of the checkboxes being checked when the submit button is pressed. If so, the value of the checkbox is added to the mixPrice variable, which is used to calculate the total cost. Hope this helps.

 

Question 2: To move on from this form, you will need some server side script (e.g. PHP) to process the information, e.g. how many mixes were selected and the total price. Here are some links:

 

http://www.tizag.com/phpT/forms.php

http://webcheatsheet.com/PHP/form_processing.php

 

Question 3: Is this something that will be implemented in to the main form. e.g. I select 4 mixes but I want only Acapella Mix for 1? If so, you may need to rework the form ...you could use AJAX for this; display additional form fields depending on the number entered by the user in the first box...if you get me!

  • Author

AJAX!!! I've never heard of that in my life before but it does exactly what I hoped someone would suggest!

Thanks Lyndsey, I've used PHP in a contact form once before so I should be able to get my head around doing this with the help of those tutorials you linked. And I also found an AJAX tut on the first site you linked for PHP. I'm going to take a few days out on the tutorials and hopefully I can come back with something to make you proud haha.

And thank you so much for the checkbox update, I don't think that's something I would ever have worked out on my own!

Wish me luck!

Duan

  • Author

WOW I've spent today researching AJAX and PHP and I don't think I'm any nearer to my goal yet! Overwhelmingly new concepts to me, I've got a looong way to go!

 

Anyway I've been adjusting my "quote builder" again, and while doing so I wondered if it wouldn't be possible to get new form fields appearing using js to hide them when the page loads until the checkboxes are checked? Or is that clunky way of doing it?

Also, I wondered if you could advise me on what to use to get the dropdown boxes (once they have appeared) to populate themselves with the text inputted in the above text box. Or is that way over the top?

Here's my code so you can see what I mean: (ignore the "your price" thing, I plan to use it to replace the "Total".)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<title>Untitled Document</title>
</head>

<body>

<form id="calc" name="calc">
<p>
<H1>Mix - Quote Builder</H1>
Step 1.
</p>
<p>Please input the number of songs you wish to submit for mixing. </p>
<p>
      <input id="mixes" name="mixes" type="text" />
      <label for="mixes">Number of mixes</label>
<p>Please select the optional extras that you require.</p>
<p>
  <input type="checkbox" name="extras" value="25" class="chk-option" />  <label for "mixes">24bit Stems (ready for mastering)</label>
</p>  
<form action="">
<select name="options">
<option value="All">All</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
</form>
<p>
  <input type="checkbox" name="extras" value="25" class="chk-option" />  <label for "mixes">A Capella Mix</label>
</p>  
<form action="">
<select name="options">
<option value="All">All</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
</form>
<p>
  <input type="checkbox" name="extras" value="25" class="chk-option" />
  Instrumental Mix
</p>  
<form action="">
<select name="options">
<option value="All">All</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
</form>
</p>
<p>
    <input name="calculateTotal" id="calculateTotal" type="button" value="Calculate Total" />          
</p>
    <div id="result" style="clear:both;">
        <strong>Total:</strong> 
        <span id="total"></span> 
        <strong>Per mix:</strong> 
        <span id="permix"></span>
   </div>
  
</form>
<p>
<table class="table">
<tr class="table-firstrow"><th>Your Price</th><th> </th></tr>
 <tr><td>Order Code</td><td>example</td></tr>
 <tr><td>Number of mixes</td><td>0</td></tr>
 <tr><td>Optional Extras</td><td>0</td></tr>
 <tr><td>Total</td><td>0</td></tr>
</table>
</p>
<p>
<FORM action="mixquotebuilderstep2.html" target="_blank">
<INPUT type=submit value="Proceed">
</FORM>
</p>
<script>
$(document).ready(function() {
	$('#calculateTotal').click(function() {
            //individual mix price before reduction
            var mixPriceIndiv = 50;
            //number of mixes selected by user
            var nummixes = parseInt($('#mixes').val());
            //get mix price based on number of mixes 
            var mixPrice = getmixPrice(nummixes);           

            //for each of the chk-option checkboxes, add values if checked
            $('.chk-option').each(function() {  
                if( $(this).is(':checked') ) {
                      mixPrice+= parseInt($(this).val());              
                }
             });
           
            //grand total
            $('#total').html('£' + mixPrice.toFixed(2));        
            
            //per mix
            $('#permix').html('£' + mixPriceIndiv.toFixed(2));			
	});
    
    function getmixPrice(el) {
        numS = parseInt(el);        
        //calculate mix price
        shPrice = 50*numS;        
        //get percentage value based on number of mixes e.g. if 2 mixes, take off 1%
        percentVal = (el!==1) ? ((numS-1)/100)*shPrice : 1                      
        //get mix price minus the percentage
        mixPrice = (el!==1) ? (50*el) - percentVal : 50   
        //console.log(mixPrice);
        return mixPrice;
    }
});
  </script>
</body>
</html>




ANY advice is gratefully received, as always!

Duan

You're only going to be rendering three select boxes for the three checkboxes, so I wouldn't bother with AJAX, it's not that much content to render. So, I'd go with having them on the page when it loads, but hidden. When the user checks the checkbox, unhide the corresponding select.

 

Also, you don't need to create a new form for each select. You've already got your form tags open and nesting forms isn't allowed. I'll try and put something together for you, but I don't know how long I'll be (pretty busy). I'll get back to you :)

 

In the meantime, I'd continue reading and using PHP to get to grips with how you use it with forms.

 

Lyndsey.

Hey Duan,

 

Here's a little update. http://jsfiddle.net/XwJrJ/7/

 

What I've done is add a function that generates the select options based on the number of mixes entered. If I enter 4, the Options 1-4 will be generated in the select boxes. The boxes are hidden by default using the CSS, but are shown when their corresponding checkbox is checked.

 

Go through the HTML and JS to see what I've changed and if you've got any questions, ask away.

 

How's your PHP coming along?

 

Edit: Oops, just realised that the selects are showing when there's only one mix selected, find this:

 

//show the corresponding select when the checkbox is clicked...    
$('.chk-option').click(function() {  
    //hide the select if its already visible...
    $('select[data-role="'+$(this).attr("name")+'"]').toggle();        
});

And change it to:

 

$('.chk-option').click(function() {
  if(parseInt($('#mixes').val())>1) {
        $('select[data-role="'+$(this).attr("name")+'"]').toggle();
  }
});

It's just a conditional statement that only runs the select toggle when the number of mixes selected is greater than 1. Simple really.

Edited by Lyndsey

  • Author

You're a genius! I actually cheered out loud the first time the dropdown box appeared with the corresponding number of options haha! Awesome!

Thanks so much, every now and then I go and pretend I'm using it for real haha.

Anyway, I got reading a PHP walk-through and read the whole thing. But it was only once I'd finished that I realised I only understood like the first couple of paragraphs and I must have spent the rest of the time just kidding myself that I understood what it all meant. But today with each meal I've watched youtube vids on the basics and I'm starting to get to grips with the syntax rules and getting my head around client side and server side and stuff. And I've got some free hosting space now to test out posting and echoing and all this other stuff I'm not sure I understand enough to be doing!

My HTML understanding is improving with all of this as well, now I know nesting forms is illegal :D

 

So here's the page online: http://www.ickeck.comze.com/quote.html

What has it done to my "£25"?

Here it is in JFiddle to see if I've changed something by accident: http://jsfiddle.net/Duan_d/22Hu2/



Cheers,

Duan.

Edited by Duan_D

No problem, I find I learn more myself by doing things like this :)

 

Keep practising :p Here are some useful sites:

 

http://php.net/manual/en/index.php

http://www.codecademy.com/learn

http://www.w3schools.com/php/default.asp

 

Regarding the live site, I think the '£' issue is in relation to a charset, though I'm not 100% sure. To be on the safe side, use the HTML code for the symbol instead, to be sure it doesn't conflict with anything. The code is: £ (I've added it in to the jQuery if you didn't notice earlier). Here is a page listing some HTML codes, may be useful: http://www.ascii.cl/htmlcodes.htm

 

Best of luck :)

  • Author

HI!

An update,

So I've been solidly working through tutorials on http://thephpbasics.com/ and reading http://www.w3schools.com/ and I've got a much clearer understanding of how HTML and PHP interact now. I've now got onto the tutorials for MYSQL but I'm feeling a little confused. All the tutorials seem to be written in a way that teach you all the functions etc but I still have very little understanding of how these things can be applied practically.

So I think I know what I want for my website, but I'm not really closer to understanding how to achieve the results.. which development tools do I use/learn to get which results?

So I'm hoping you'll be able to point me in the right direction again, even if you just focus my thoughts a little.

Here's my site as it is at the moment: http://www.ickeck.comze.com/index.php

 

Oh also, by the way, how come other websites don't tend to have .php extensions, they always have html. Is the use of "include" and "require" PHP for headers and footers not a common thing, or do they just save the files as .html anyway?

 

So now I need to get from having the calculator outputting a value, to using that value in a confirmation and then transaction (paypal to begin with most likely?). Then, having completed the transaction, the user needs to get to a page populated with the appropriate number of file upload boxes (each accompanied with a text input), or a checkbox saying they will send a CD in the post containing their unmixed product.

Does the fact that I need so much information remembered mean I need to carry on learning about MSQL?

Is it possible to look at my needs and recommend any specific functions in any specific tool?


I'm hoping you can offer some relief haha I'm so muddled!

Cheers,
Duan

Edited by Duan_D

 

So I've been solidly working through tutorials on http://thephpbasics.com/ and reading http://www.w3schools.com/ and I've got a much clearer understanding of how HTML and PHP interact now. I've now got onto the tutorials for MYSQL but I'm feeling a little confused. All the tutorials seem to be written in a way that teach you all the functions etc but I still have very little understanding of how these things can be applied practically.

 

So I think I know what I want for my website, but I'm not really closer to understanding how to achieve the results.. which development tools do I use/learn to get which results?

Hey Duan,

As you're collecting user data, you'll need a Server Side language that allows this (PHP). It just allows us to interact with our users and we use it to collect data from them. If we want to store this user data, for example we want users to sign up to our website, then we will need a database. This is where MySQL comes in to play... it's basically a database management system.

If you're running PHP locally on your machine, you'll need to download WAMP server, which comes with everything you need to begin creating your applications (an Apache server, phpMyAdmin and a mySQL database. PhpMyAdmin is the application that allows us to create databases and store our info.

 

Oh also, by the way, how come other websites don't tend to have .php extensions, they always have html. Is the use of "include" and "require" PHP for headers and footers not a common thing, or do they just save the files as .html anyway?

Websites with a generic .html page extension will be static pages. It may be the case that the server side interactions are taking place externally. For example, you have a HTML form (.html page) that is processed by a PHP page, but the user is redirected back to the HTML page upon completion, so you never actually get to see the PHP file in effect. Hope that makes sense! It just means that the PHP and HTML are kept in separate files.

 

Does the fact that I need so much information remembered mean I need to carry on learning about MSQL?

Only if you want to store the information in a database. Otherwise, HTML and PHP will do.

 

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.