Solved

Function not defined

Posted on 2007-12-06
17
252 Views
Last Modified: 2008-02-01
If you go to this page - http://rksdesignstudios.com/files/project/htm/?p=trinkets

And try to add to cart and click inside the input box I get errors:

addCart is not defined
http://localhost/rks/clients/project/htm/?p=bowls
Line 1

clearValue is not defined
http://localhost/rks/clients/project/htm/?p=bowls
Line 1

Also get it remotely If you are noticing the address..i have been checking my HTML/JS and cant see why..any suggestions?

http://rksdesignstudios.com/files/project/common/shopping_cart.js is my JS file

Thanks,

Ryan
0
Comment
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 5
  • 3
17 Comments
 
LVL 8

Expert Comment

by:kebabs
ID: 20425276
Something is wrong here (line96-98 of shopping_cart.js). The first function does not have a closing bracket nor does it have any statements.
function checkForm(){
 
function clearValue(n){

Open in new window

0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 20425320
I see so here it is. There is a closing } for checkForm() correct? And I tried adding in the clear value one and got an error.

Thanks!
Ryan
function checkForm(){
 
function clearValue(n){
    if(n == 0)
    {
        document.order0.p0.value = "";
    }
    else if(n == 1)
    {
        document.order1.p1.value = "";
    }
    else if(n == 2)
    {
        document.order2.p2.value = "";
    }
    else
    {
        document.order3.p3.value = "";
    }
 
}

Open in new window

0
 
LVL 8

Expert Comment

by:kebabs
ID: 20425333
No, the problem is that checkForm() is missing. A temporary workaround is this:
function checkForm(){
    return true;
}
 
function clearValue(n){
    if(n == 0)
    {
        document.order0.p0.value = "";
    }
    else if(n == 1)
    {
        document.order1.p1.value = "";
    }
    else if(n == 2)
    {
        document.order2.p2.value = "";
    }
    else
    {
        document.order3.p3.value = "";
    }
 
}

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Expert Comment

by:TheGD
ID: 20426666
Yes right the braces seems to be misplaced. Just replace your "shopping_cart.js" file with the following code and see if this has solved your problem.
/* 
 
Shopping Cart - Woodworking
 
####   Function List (in order - top to bottom):    ####
 
findCookie
makeOrderCookie
deleteProduct
makeFormCookie
zapOrder
checkForm
clearValue
addCart
showOrder
confirmOrder
 
####   Reference to variable names                   ####
 
cartItem       = cookie name for storing item
deleteCookieOn = delete cookie date
 
*/
 
var now = new Date();
var expire = new Date(now.getTime() + 14 * 24 * 60 * 60 * 1000);
function findCookie(val) {
	// set cookie default
    var cookie = null;
    var findVal = val + "=";
    var dc = document.cookie;
    if (dc.length > 0)
    {
        var start = dc.indexOf(findVal);
        if (start >= 0)
        {
            start += findVal.length;
            lastVal = dc.indexOf(";", start);
            if (lastVal == -1)
            {
                lastVal = dc.length;
            }
            cookie = (dc.substring(start, lastVal));
        }
        else
        {
            return cookie;
        }
    }
    return cookie;
}
function makeOrderCookie(n,id,product_name,price,q){
    document.cookie = "cartItem"+n+" = "+id+","+product_name+","+price+","+q+";expires = "
    +expire.toGMTString();
    alert("You have added "+q+" of the "+product_name+" to your shopping cart.");
}
function deleteProduct(product_name){
    document.cookie = "cartItem"+product_name+" = ;expires = Thu, 01-Jan-70 00:00:01 GMT;";
    location.reload();
}
function makeFormCookie(c0,c1,c2,c3,c4,c5,c6,c7){
 
    var form = new Array();
    form[0] = c0;
    form[1] = c1;
    form[2] = c2;
    form[3] = c3;
    form[4] = c4;
    form[5] = c5;
    form[6] = c6;
    form[7] = c7;
    for(i=0;i<9;i++)
    {
        document.cookie = "c"+[i]+" = "+form[i]+";expires = "+expire.toGMTString();
    }
    // redirect to checout page
    window.location = "?p=checkout.php"
}
c0 = findCookie("c0");
c1 = findCookie("c1");
c2 = findCookie("c2");
c3 = findCookie("c3");
c4 = findCookie("c4");
c5 = findCookie("c5");
c6 = findCookie("c6");
c7 = findCookie("c7");
function zapOrder(){
    deleteCookieOn = "Thursday, 01-Jan-70 12:00:01 GMT";
    for (i=0;i<=4;i++) {
        document.cookie = "cartItem"+[i]+" = ; expires = "+deleteCookieOn+";";
    }
    for (i=0;i<=10;i++) {
        document.cookie = "c"+[i]+" = ; expires = "+deleteCookieOn+";";
    }
}
function checkForm(){}
function clearValue(n){
    if(n == 0)
    {
        document.order0.p0.value = "";
    }
    else if(n == 1)
    {
        document.order1.p1.value = "";
    }
    else if(n == 2)
    {
        document.order2.p2.value = "";
    }
    else
    {
        document.order3.p3.value = "";
    }
 
}
/*
REFERENCE
 
Product Id   = id
Product Name = product_name
Price        = price
 
 
document.form_name.field_name.value;
 
*/
 
 
function addCart(id,product_name,price,n){
    switch(n)
    {
        case 1:
        q = document.order0.p0.value;
        break;
        case 2:
        q = document.order1.p1.value;
        break;
        case 3:
        q = document.order2.p2.value;
        break;
        case 4:
        q = document.order3.p3.value;
    }
    if(q == 0)
    {
        alert("Must be higher than one.");
        return false;
    }
    makeOrderCookie(n,id,product_name,price,q);
}
function showOrder(){
    var subTotal = 0; // set subtotal price
    var product_name = new Array();
    product_name[0] = findCookie("cartItem0");
    product_name[1] = findCookie("cartItem1");
    product_name[2] = findCookie("cartItem2");
    product_name[3] = findCookie("cartItem3");
    if((product_name[0] == null)&&(product_name[1] == null)&&(product_name[2] == null)&&(product_name[3] == null))
    {
        document.write("<tr><td>");
        document.write("Nothing in your cart! Use our menu to go buy a product.");
        document.write("</td></tr>");
    }
    for(i=0;i<=product_name.length;i++)
    {
        if((product_name[i] != null)&&(product_name[i] != ""))
        {
            document.write("<tr><td>");
            start = product_name[i].substring(0,4);
            document.write(start+"</td><td>");
            name = product_name[i].indexOf(",")+1;
            name2 = product_name[i].indexOf(",",name);
            product_name_name = product_name[i].substring(name,name2);
            document.write(product_name_name+"</td><td class=right'>");
            priceice = product_name[i].indexOf(",",name2)+1;
            priceice2 = product_name[i].indexOf(",",priceice);
            product_name_priceice = parseFloat(product_name[i].substring(priceice,priceice2));
            priceice = product_name_priceice.toFixed(2);
            document.write("$"+priceice+"</td><td class='center'>");
            quant = parseInt(product_name[i].substring(product_name[i].length-1,product_name[i].length));
            document.write(quant+"</td><td class='right'>");
            cost_raw = quant*product_name_priceice;
            cost_tot = cost_raw;
            subTotal += cost_tot;
            cost = cost_raw.toFixed(2);
            document.write("$"+cost+"</td><td>");
            document.write("<a href=# uuups = 'delete("+[i]+")'>Remove Item</a>");
            document.write("</td><td>");
        }
    }
    document.write("<tr><td colspan = '5' class='right'>The total cost of your order is <b>$"+subTotal.toFixed(2)+"</b></td></tr>");
}
function confirmForm(){
    fn = document.order.fn.value;
    ln = document.order.ln.value;
    c0 = document.order.add.value;
    c1 = document.order.city.value;
    c2 = document.order.st.value;
    c3 = document.order.zip.value;
    c = -1
    for(i = 0; i<document.order.pay.length; i++)
    {
        if(document.order.pay[i].checked)
        {
            c = i;
            c4 = document.order.pay[i].value;
        }
    }
    c5 = document.order.card.value;
    c6 = document.order.month.value;
    c7 = document.order.year.value;
    if((fn == "")||(ln == "")||(c0 == "")||(c1 == "")||(c2 == "")||
       (c3 == "")||(c4 == "")||(c5 == "")||(c6 == "")||(c7 == ""))
    {
        alert("Please complete all parts of this form.");
        return false;
    }
    makeFormCookie(fn,ln,c0,c1,c2,c3,c4,c5,c6,c7);
}
if(c0 == null)
{
    c0 = "";
}
if(c1 == null)
{
    c1 = "";
}
if(c2 == null)
{
    c2 = "";
}
if(c3 == null)
{
    c3 = "";
}
if(c4 == null)
{
    c4 = "";
}
if(c5 == null)
{
    c5 = "";
}
if(c6 == null)
{
    c6 = "";
}
if(c7 == null)
{
    c7 = "";
}

Open in new window

0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 20427043
Hey man..thanks for that! It got rid of the error..but now I get:

if(q == 0)
    {
        alert("Must be higher than one.");
        return false;
    }


q not defined on Line 144. Not sure why..again all of my names match up. Q being the quantity.

Ryan
0
 
LVL 8

Expert Comment

by:kebabs
ID: 20427080
Replace addCart() function with this.

BTW, I'd consider finding the original version of this script as these workarounds will not be perfect.
function addCart(id,product_name,price,n){
    var q;
    switch(n)
    {
        case 1:
        q = document.order0.p0.value;
        break;
        case 2:
        q = document.order1.p1.value;
        break;
        case 3:
        q = document.order2.p2.value;
        break;
        case 4:
        q = document.order3.p3.value;
    }
    if(q == 0)
    {
        alert("Must be higher than one.");
        return false;
    }
    makeOrderCookie(n,id,product_name,price,q);
}

Open in new window

0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 20427530
Alrighty..i have it handy. Faily new at JS wanted to get more familiar with it.

I did that and It is adding and creating the cookie.

After doing some more editing  -- if you go here: http://rksdesignstudios.com/files/project/htm/?p=trinkets

And try adding the first one you get undefined. If you go to the second one and type in say 5, it works..any ideas why?

Below is my makeCookieOrder that is apart of the addCart function.

And on here - http://rksdesignstudios.com/files/project/htm/?p=cart

I dont know why it is outputting all of that.
Ryan
function makeFormCookie(c0,c1,c2,c3,c4,c5,c6,c7){
    var form = new Array();
    form[0] = c0;
    form[1] = c1;
    form[2] = c2;
    form[3] = c3;
    form[4] = c4;
    form[5] = c5;
    form[6] = c6;
    form[7] = c7;
    for(i=0;i<9;i++)
    {
        document.cookie = "c"+[i]+" = "+form[i]+";expires = "+expire.toGMTString();
    }
    // redirect to checout page
    window.location = "?p=checkout.php"
}
c0 = findCookie("c0");
c1 = findCookie("c1");
c2 = findCookie("c2");
c3 = findCookie("c3");
c4 = findCookie("c4");
c5 = findCookie("c5");
c6 = findCookie("c6");
c7 = findCookie("c7");

Open in new window

0
 
LVL 5

Expert Comment

by:TheGD
ID: 20427559
- The addCart function needs fine tuning, just by defining the "var q;" is not enough.
- The phrase "Must be higher than one." should be "Must be higher than or equal to one." Because input of 1 or greater is acceptable and less than 1 un-acceptable.
- If the function returns false, then it should return the true also.

Here is the solution to your problem, replace the addCart function as follows:
function addCart(id,product_name,price,n){
    var q;
    switch(n)
    {
        case 1:
        q = parseInt(document.order0.p0.value);
        break;
        case 2:
        q = parseInt(document.order1.p1.value);
        break;
        case 3:
        q = parseInt(document.order2.p2.value);
        break;
        case 4:
        q = parseInt(document.order3.p3.value);
    }
    if(q == 0)
    {
        alert("Must be higher or equal than one.");
        return false;
    }
    makeOrderCookie(n,id,product_name,price,q);
    return true;
}

Open in new window

0
 
LVL 5

Expert Comment

by:TheGD
ID: 20427595
Keep the other script as is, and now just update the addCart function as stated above. It would solve your problem and wont say undefined any more.
0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 20427612
Wow...you brought up a really really good/important point of that script..you are right. I made those changes and still get undefined on the first product not sure why..I reuploaded my code as well.

All of the parameters of the function seem to match up and everything.


Also  I have this -  http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_23008630.html
0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 20427617
0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 20427678
If it helps here is my HTML..if I can do anything else..let me know!

Thanks,

Ryan

		<h1>Trinkets</h1>
		
		<div class="imgOver">
			<img src="../images/vase1_sm_50.jpg" name="show">
		</div>
		
		<div class="product">
			<img src="../images/vase1_sm_50.jpg" alt="product" width="58" class="imgLeft" onmouseover="show.src=blue_vase.src"/>
			<p>
				<h2>Blue Vase - $50.00</h2>
				<h3>Product #1</h3>
				This beautiful vase will enhance any bouquet. The perfect complement to any dinner table.	
			</p>
			<form name="order0">
			 	<label>Quantity:</label>
             	<input name="p0" size="1" value="1" maxlength="1" onfocus="clearValue('0')" />
             	<input type="button" value="Add to Cart" onclick="addCart('1','Blue Vase','50.00',0)" />
        	</form>
		</div>
		<div class="product">
			<img src="../images/vase2_sm_50.jpg" alt="product" class="imgLeft" onmouseover="show.src=green_vase.src"/>
			<p>
				<h2>Green Vase - $25.00</h2>
				<h3>Product #2</h3>
				The perfect gift for storing fine jewelry and keepsakes.	
			</p>
			<form name="order1">
			 	<label>Quantity:</label>
             	<input name="p1" size="1" value="1" maxlength="1" onfocus="clearValue('1')" />
             	<input type="button" value="Add to Cart" onclick="addCart('2','Green Vase','25.00',1)" />
        	</form>
		</div>
		<div class="product">
			<img src="../images/box_sm_50.jpg" alt="product" class="imgLeft" onmouseover="show.src=box.src"/>
			<p>
				<h2>Box - $5.00</h2>
				<h3>Product #3</h3>
				The perfect gift for storing fine jewelry and keepsakes.
			</p>
			<form name="order2">
			 	<label>Quantity:</label>
             	<input name="p2" size="1" value="1" maxlength="1" onfocus="clearValue('2')" />
             	<input type="button" value="Add to Cart" onclick="addCart('3','Box','5.00',2)" />
        	</form>
		</div>
		<div class="product">
			<img src="../images/candlesticks_sm_50.jpg" alt="product" class="imgLeft" onmouseover="show.src=candle_sticks.src"/>
			<p>
				<h2>Candlesticks - $10.00</h2>
				<h3>Product #4</h3>
				These antique candlestick holders come with a certificate of authenticity.	
			</p>
			<form name="order3">
			 	<label>Quantity:</label>
             	<input name="p3" size="1" value="1" maxlength="1" onfocus="clearValue('3')" />
             	<input type="button" value="Add to Cart" onclick="addCart('4','Candlesticks','10.00',3)" />
        	</form>
		</div>

Open in new window

0
 
LVL 5

Expert Comment

by:TheGD
ID: 20428464
Replace addCart function with this one of mine and your problem is solved, believe me :)
function addCart(id,product_name,price,n){
    var q;
    switch(n)
    {
        case 1:
        q = parseInt(document.order0.p0.value);
        break;
        case 2:
        q = parseInt(document.order1.p1.value);
        break;
        case 3:
        q = parseInt(document.order2.p2.value);
        break;
        case 4:
        q = parseInt(document.order3.p3.value);
    }
    if(q == 0)
    {
        alert("Must be one or greater value.");
        return false;
    }
    makeOrderCookie(n,id,product_name,price,q);
    return true;
}

Open in new window

0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 20428664
Still get the same thing:

http://rksdesignstudios.com/files/project/htm/?p=trinkets

No clue why..when everything seems to match up. Clueless. The first item on the page seems to mess up and not sure why.

I have it starting at 0 form form names/ids because im using arrays. Then the second item will add to cart and show quantity.

Ryan
/*
Shopping Cart - Woodworking
 
####  Function List (in order - top to bottom): ####
 
 
findCookie
makeOrderCookie
deleteProduct - deletes product
makeFormCookie
zapOrder
checkForm
clearValue  - starts val at zero used as reset
addCart  - adds product to cart
showOrder - prints order on cart.php page
confirmOrder - used on confirm page
 
 
####  Reference to variable names ####
 
cartItem       = cookie name for storing item
deleteCookieOn = delete cookie date
Total Cost     = total_cost
Subtotal Cost  = subtotal_cost
 
 
*/
var now = new Date();
// used for cookie.expire
var expire = new Date(now.getTime() + 14 * 24 * 60 * 60 * 1000);
/************************************************************\
*
\************************************************************/
function findCookie(val) {
    // set cookie default
    var cookie = null;
    var findVal = val + "=";
    var dc = document.cookie;
    if (dc.length > 0)
    {
        var start = dc.indexOf(findVal);
        if (start >= 0)
        {
            start += findVal.length;
            lastVal = dc.indexOf(";", start);
            if (lastVal == -1)
            {
                lastVal = dc.length;
            }
            cookie = (dc.substring(start, lastVal));
        }
        else
        {
            return cookie;
        }
    }
    return cookie;
}
/************************************************************\
*
\************************************************************/
function makeOrderCookie(n,id,product_name,price,q){
    document.cookie = "cartItem"+n+" = "+id+","+product_name+","+price+","+q+";expires = "
    +expire.toGMTString();
    alert("You have added "+q+" of the "+product_name+" to your shopping cart.");
}
/************************************************************\
*
\************************************************************/
function deleteProduct(product_name){
    document.cookie = "cartItem"+product_name+" = ;expires = Thu, 01-Jan-70 00:00:01 GMT;";
    location.reload();
}
/************************************************************\
*
\************************************************************/
function makeFormCookie(c0,c1,c2,c3,c4,c5,c6,c7){
    var form = new Array();
    form[0] = c0;
    form[1] = c1;
    form[2] = c2;
    form[3] = c3;
    form[4] = c4;
    form[5] = c5;
    form[6] = c6;
    form[7] = c7;
    for(i=0;i<9;i++)
    {
        document.cookie = "c"+[i]+" = "+form[i]+";expires = "+expire.toGMTString();
    }
    // redirect to checout page
    window.location = "?p=checkout.php"
}
c0 = findCookie("c0");
c1 = findCookie("c1");
c2 = findCookie("c2");
c3 = findCookie("c3");
c4 = findCookie("c4");
c5 = findCookie("c5");
c6 = findCookie("c6");
c7 = findCookie("c7");
/************************************************************\
*
\************************************************************/
function zapOrder(){
    deleteCookieOn = "Thursday, 01-Jan-70 12:00:01 GMT";
    for (i=0;i<=4;i++) {
        document.cookie = "cartItem"+[i]+" = ; expires = "+deleteCookieOn+";";
    }
    for (i=0;i<=10;i++) {
        document.cookie = "c"+[i]+" = ; expires = "+deleteCookieOn+";";
    }
}
/************************************************************\
*
\************************************************************/
function checkForm(){}
/************************************************************\
*
\************************************************************/
function clearValue(n){
    if(n == 0)
    {
        document.order0.p0.value = "";
    }
    else if(n == 1)
    {
        document.order1.p1.value = "";
    }
    else if(n == 2)
    {
        document.order2.p2.value = "";
    }
    else
    {
        document.order3.p3.value = "";
    }
}
/*
REFERENCE
 
Product Id    = id
Product Name  = product_name
Price         = price
 
 
document.form_name.field_name.value;
*/
/************************************************************\
*
\************************************************************/
function addCart(id,product_name,price,n){
    var q;
    switch(n)
    {
        case 1:
        q = parseInt(document.order0.p0.value);
        break;
        case 2:
        q = parseInt(document.order1.p1.value);
        break;
        case 3:
        q = parseInt(document.order2.p2.value);
        break;
        case 4:
        q = parseInt(document.order3.p3.value);
    }
    if(q == 0)
    {
        alert("Must be one or greater value.");
        return false;
    }
    makeOrderCookie(n,id,product_name,price,q);
    return true;
}
/************************************************************\
*
\************************************************************/
function showOrder(){
    var subTotal = 0;                // set subtotal price
    var product_name = new Array();  // place products in array
    product_name[0] = findCookie("cartItem0");
    product_name[1] = findCookie("cartItem1");
    product_name[2] = findCookie("cartItem2");
    product_name[3] = findCookie("cartItem3");
    if((product_name[0] == null)&&(product_name[1] == null)&&(product_name[2] == null)&&(product_name[3] == null))
    {
        document.write("<tr><td>");
        document.write("Nothing in your cart! Use our menu to go buy a product.");
        document.write("</td></tr>");
    }
    for(i=0;i<=product_name.length;i++)
    {
        if((product_name[i] != null)&&(product_name[i] != ""))
        {
            document.write("<tr><td>");
            start = product_name[i].substring(0,4);
            document.write(start+"</td><td>");
            name = product_name[i].indexOf(",")+1;
            name2 = product_name[i].indexOf(",",name);
            product_name_name = product_name[i].substring(name,name2);
            document.write(product_name_name+"</td><td class=right'>");
            priceice = product_name[i].indexOf(",",name2)+1;
            priceice2 = product_name[i].indexOf(",",priceice);
            product_name_priceice = parseFloat(product_name[i].substring(priceice,priceice2));
            priceice = product_name_priceice.toFixed(2);
            document.write("$"+priceice+"</td><td class='center'>");
            quant = parseInt(product_name[i].substring(product_name[i].length-1,product_name[i].length));
            document.write(quant+"</td><td class='right'>");
            cost_raw = quant*product_name_priceice;
            total_cost = cost_raw;
            subTotal += total_cost;
            cost = cost_raw.toFixed(2);
            document.write("$"+cost+"</td><td>");
            document.write("<a href=# onclick = 'delete("+[i]+")'>Remove Item</a>");
            document.write("</td><td>");
        }
    }
    document.write("<tr><td colspan = '5' class='right'>The total cost of your order is <b>$"+subTotal.toFixed(2)+"</b></td></tr>");
}
/************************************************************\
*
\************************************************************/
function confirmForm(){
    fn = document.order.fn.value;
    ln = document.order.ln.value;
    c0 = document.order.add.value;
    c1 = document.order.city.value;
    c2 = document.order.st.value;
    c3 = document.order.zip.value;
    c = -1
    for(i = 0; i<document.order.pay.length; i++)
    {
        if(document.order.pay[i].checked)
        {
            c = i;
            c4 = document.order.pay[i].value;
        }
    }
    c5 = document.order.card.value;
    c6 = document.order.month.value;
    c7 = document.order.year.value;
    if((fn == "")||(ln == "")||(c0 == "")||(c1 == "")||(c2 == "")||
       (c3 == "")||(c4 == "")||(c5 == "")||(c6 == "")||(c7 == ""))
    {
        alert("Please complete all parts of this form.");
        return false;
    }
    makeFormCookie(fn,ln,c0,c1,c2,c3,c4,c5,c6,c7);
}
if(c0 == null){
    c0 = "";
}
if(c1 == null){
    c1 = "";
}
if(c2 == null){
    c2 = "";
}
if(c3 == null){
    c3 = "";
}
if(c4 == null){
    c4 = "";
}
if(c5 == null){
    c5 = "";
}
if(c6 == null){
    c6 = "";
}
if(c7 == null){
    c7 = "";
}

Open in new window

0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 20428798
Here is my updated code:

/*
Shopping Cart - Woodworking
 
####  Function List (in order - top to bottom): ####
 
 
findCookie
makeOrderCookie
deleteProduct - deletes product
makeFormCookie
zapOrder
checkForm
clearValue  - starts val at zero used as reset
addCart  - adds product to cart
showOrder - prints order on cart.php page
confirmOrder - used on confirm page
 
 
####  Reference to variable names ####
 
cartItem       = cookie name for storing item
deleteCookieOn = delete cookie date
Total Cost     = total_cost
Subtotal Cost  = subtotal_cost
Quantity       = quantity
 
 
*/
var now = new Date();
// used for cookie.expire
var expire = new Date(now.getTime() + 14 * 24 * 60 * 60 * 1000);
/************************************************************\
*
\************************************************************/
function findCookie(val) {
    // set cookie default
    var cookie = null;
    var findVal = val + "=";
    var dc = document.cookie;
    if (dc.length > 0)
    {
        var start = dc.indexOf(findVal);
        if (start >= 0)
        {
            start += findVal.length;
            lastVal = dc.indexOf(";", start);
            if (lastVal == -1)
            {
                lastVal = dc.length;
            }
            cookie = (dc.substring(start, lastVal));
        }
        else
        {
            return cookie;
        }
    }
    return cookie;
}
/************************************************************\
*
\************************************************************/
function makeOrderCookie(n,id,product_name,price,q){
    document.cookie = "cartItem"+n+" = "+id+","+product_name+","+price+","+q+";expires = "
    +expire.toGMTString();
    alert("You have added "+q+" of the "+product_name+" to your shopping cart.");
}
/************************************************************\
*
\************************************************************/
function deleteProduct(product_name){
    document.cookie = "cartItem"+product_name+" = ;expires = Thu, 01-Jan-70 00:00:01 GMT;";
    location.reload();
}
/************************************************************\
*
\************************************************************/
function makeFormCookie(c0,c1,c2,c3,c4,c5,c6,c7){
    var form = new Array();
    form[0] = c0;
    form[1] = c1;
    form[2] = c2;
    form[3] = c3;
    form[4] = c4;
    form[5] = c5;
    form[6] = c6;
    form[7] = c7;
    for(i=0;i<9;i++)
    {
        document.cookie = "c"+[i]+" = "+form[i]+";expires = "+expire.toGMTString();
    }
    // redirect to checout page
    window.location = "?p=checkout.php"
}
c0 = findCookie("c0");
c1 = findCookie("c1");
c2 = findCookie("c2");
c3 = findCookie("c3");
c4 = findCookie("c4");
c5 = findCookie("c5");
c6 = findCookie("c6");
c7 = findCookie("c7");
/************************************************************\
*
\************************************************************/
function zapOrder(){
    deleteCookieOn = "Thursday, 01-Jan-70 12:00:01 GMT";
    for (i=0;i<=4;i++) {
        document.cookie = "cartItem"+[i]+" = ; expires = "+deleteCookieOn+";";
    }
    for (i=0;i<=10;i++) {
        document.cookie = "c"+[i]+" = ; expires = "+deleteCookieOn+";";
    }
}
/************************************************************\
*
\************************************************************/
function checkForm(){}
/************************************************************\
*
\************************************************************/
function clearValue(n){
    if(n == 0)
    {
        document.order0.p0.value = "";
    }
    else if(n == 1)
    {
        document.order1.p1.value = "";
    }
    else if(n == 2)
    {
        document.order2.p2.value = "";
    }
    else
    {
        document.order3.p3.value = "";
    }
}
/*
REFERENCE
 
Product Id    = id
Product Name  = product_name
Price         = price
 
 
document.form_name.field_name.value;
*/
/************************************************************\
*
\************************************************************/
function addCart(id,product_name,price,n){
    var q;
    switch(n)
    {
        case 1:
        q = parseInt(document.order0.p0.value);
        break;
        case 2:
        q = parseInt(document.order1.p1.value);
        break;
        case 3:
        q = parseInt(document.order2.p2.value);
        break;
        case 4:
        q = parseInt(document.order3.p3.value);
    }
    if(q == 0)
    {
        alert("Must be one or greater value.");
        return false;
    }
    makeOrderCookie(n,id,product_name,price,q);
    return true;
}
/************************************************************\
*
\************************************************************/
function showOrder(){
    var subTotal = 0;                // set subtotal price
    var product_name = new Array();  // place products in array
    product_name[0] = findCookie("cartItem0");
    product_name[1] = findCookie("cartItem1");
    product_name[2] = findCookie("cartItem2");
    product_name[3] = findCookie("cartItem3");
    if((product_name[0] == null)&&(product_name[1] == null)&&(product_name[2] == null)&&(product_name[3] == null))
    {
        document.write("<tr><td>");
        document.write("Nothing in your cart! Use our menu to go buy a product.");
        document.write("</td></tr>");
    }
    for(i=0;i<=product_name.length;i++)
    {
        if((product_name[i] != null)&&(product_name[i] != ""))
        {
            name              = product_name[i].indexOf(",")+1;
            name2             = product_name[i].indexOf(",",name);
            product_name_name = product_name[i].substring(name,name2);
            document.write("<h3>"+product_name_name+"</h3><br />");
            priceice          = product_name[i].indexOf(",",name2)+1;
            priceice2         = product_name[i].indexOf(",",priceice);
            product_name_priceice = parseFloat(product_name[i].substring(priceice,priceice2));
            priceice = product_name_priceice.toFixed(2);
            document.write("<b>Cost:</b> $"+priceice+"<br />");
            quantity = parseInt(product_name[i].substring(product_name[i].length-1,product_name[i].length));
            document.write("<b>Quantity:</b> "+quantity+"<br />");
            cost_raw = quantity*product_name_priceice;
            total_cost = cost_raw;
            subTotal += total_cost;
            cost = cost_raw.toFixed(2);
            document.write("<b>Cost:</b> $"+cost);
			document.write("<br /><b>Click to remove:</b> <a href=# onclick = 'deleteProduct("+[i]+")'>Remove</a><br /><br />");
	       }
    }
    document.write("<p>The total cost of your order is <b>$"+subTotal.toFixed(2)+"</p>");
}
/************************************************************\
*
\************************************************************/
function confirmForm(){
    fn = document.order.fn.value;
    ln = document.order.ln.value;
    c0 = document.order.add.value;
    c1 = document.order.city.value;
    c2 = document.order.st.value;
    c3 = document.order.zip.value;
    c = -1
    for(i = 0; i<document.order.pay.length; i++)
    {
        if(document.order.pay[i].checked)
        {
            c = i;
            c4 = document.order.pay[i].value;
        }
    }
    c5 = document.order.card.value;
    c6 = document.order.month.value;
    c7 = document.order.year.value;
    if((fn == "")||(ln == "")||(c0 == "")||(c1 == "")||(c2 == "")||
       (c3 == "")||(c4 == "")||(c5 == "")||(c6 == "")||(c7 == ""))
    {
        alert("Please complete all parts of this form.");
        return false;
    }
    makeFormCookie(fn,ln,c0,c1,c2,c3,c4,c5,c6,c7);
}
if(c0 == null){
    c0 = "";
}
if(c1 == null){
    c1 = "";
}
if(c2 == null){
    c2 = "";
}
if(c3 == null){
    c3 = "";
}
if(c4 == null){
    c4 = "";
}
if(c5 == null){
    c5 = "";
}
if(c6 == null){
    c6 = "";
}
if(c7 == null){
    c7 = "";
}

Open in new window

0
 
LVL 5

Accepted Solution

by:
TheGD earned 500 total points
ID: 20429065
I got it finally, it is addCart function again :)

Explaination:

parameter n value must be in b/w 0-3 right, because in your HTML you are callling it like that.
But your conditions were taking n vaue from 1 to 4 which was wrong. Now I has written a generic addCart funciton for you, which would use value of n rightly.
function addCart(id,product_name,price,n){
    var q, v;
    v = document["order"+n]["p"+n].value;
    if (v == "") q = 0; else q = parseInt(v);	
    if(q == 0) {
        alert("Must be one or greater value.");
        return false;
    }	
    makeOrderCookie(n,id,product_name,price,q);
    return true;
}

Open in new window

0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 20429106
Nice.thank you! Ah Ha...finally! The fix!

Appreciate your help!

Ryan
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Coding for the first time 9 65
jQuery on click requires double click 6 29
ASP get fieds value 2 21
javascript works in Chrome, but none of the other browsers 14 31
This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question