Jump to content

Recommended Posts

Hello;

 

I have a problem. The ordered stuff does not show up on my html. Here is the website where you order from: http://fhcs.be/webtech/paginas/aanbiedingen/beautifulmind.html

 

here is the js



var products = new Array();
window.addEventListener("load",showProducts,false);

window.addEventListener("load",products,false);

loadPage();

function loadPage(){
    getCookie();
}

function getCookie(){
    var elements = document.cookie.split('=');
    var products_cookie = elements[1].split('%');
    for(var i=0;i < products_cookie.length-1;i++) {
        var tmp = products_cookie[i].split('$');
        addProduct(tmp[0], tmp[1], tmp[2]);
    }
}

function setCookie(){
    var date = new Date();
    date.setTime(date.getTime()+(2592000)); //expire time = 30 dagen nadat cookie aangemaakt is
    var expires = "; expires="+date.toGMTString()
    var text = "";
    for (var i = 0; i < products.length; i++){
        text+= products[i][0] + "$" + products[i][1] + "$" + products[i][2] +"%";  // slaat naam $ aantal $ prijs op
    }
    document.cookie = "cart="+text+expires;
}

function addProduct(pName, pAmount, pPrice){
    var i = 0;
    if (products.length != 0){
        while (products[i][0] != pName && i != products.length-1){
            i++;
        }
        if (products[i][0] == pName){
            alert("verhogen");
            var tmp = parseInt(products[i][2]);
            products[i][2] += parseInt(pAmount);
        }
        else{
            alert("nieuw");
            var product = new Array(pName, parseInt(pAmount), pPrice);
            products[products.length] = product;
        }
    }
    else{
        alert("eerste");
        var product = new Array(pName, parseInt(pAmount), pPrice);
        products[products.length] = product;


    }
    setCookie();
}

function removeProduct(pName){
    for(var i = 0; i <products.length; i++){
        if((products[i][0]==pName) && (products[i][1]>1)){
            products[i][1]--;
        }
        else if(products[i][0]==pName){
            if(i==products.length-1){
                products.length--;
            }
            else{
                for(var j = i; j<products.length-1;j++){
                    products[j]= products[j+1];
                }
                products.length--;
                i = products.length;
            }

        }
    }
}

function clearAll(){

    document.cookie="cart=" + "" + -1;
    products.length = 0;
    showProducts();
}

function showProducts(){
    if (products.length =! 0){
        alert("length !=0");
        var totaal = 0;
        for (var i = 0; i < products.length; i++){
            totaal += parseInt(products[i][2])*parseFloat(products[i][1]);
            document.getElementById("shopList").innerHTML += "<ul><li>" + products[i][0] + "</li>" +
                    "<li>" + "€ " +products[i][2] + "</li>"
                    + "<li>" + products[i][1] + "</li>"
                    + "<li>" + "€ " + parseInt(products[i][2])*parseFloat(products[i][1]) + "</li></ul>";
        }
        document.getElementById("total").innerHTML = "<p>Totaal: € " + totaal + "</p>";
    }
    else{
        alert(products.length);
        document.getElementById("total").innerHTML = "";
        document.getElementById("shopList").innerHTML = "<ul><li>Er zit nog niets in het wagentje!</li></ul>";
    }
}

html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="nl" lang="nl"> 
<head> 
<link rel="stylesheet"  type="text/css" href="../stijl.css" /> 
<title>MovieStore</title> 
<meta http-equiv="content-type"
content="text/html;charset=UTF-8" /> 
<script type="text/javascript" src="../js/winkelwagen.js"></script>

</head> 
<body> 

<div id="container">

<img class="header" src="../afbeeldingen/header.png"  alt="Header"/> 

<ul id="navigatie">
	<li><a href="../index.html">Home</a></li>
	<li><a href="../paginas/aanbod.html">Aanbod</a></li>
	<li><a href="../paginas/contact.html">Contact</a></li>
	<li><a href="../paginas/login.html">Login/Register</a></li>
</ul>
<h1>Winkelwagen</h1>
<p>
<a href="../paginas/aanbod.html">Verder winkelen</a>	
</p>
     	<div id="shopList" style="color:black" >

        </div>
        <div id="total">

        </div>	
<p>
	<a href="../paginas/afrekenen.html">Afrekenen</a>

<img class="footer" src="../afbeeldingen/footer.png" alt="Footer"/>  
</p>
</div>


</body> 
</html>

html2

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="nl" lang="nl"> 
<head> 
<link rel="stylesheet"  type="text/css" href="../stijl.css" /> 
<title>Aanbod</title> 
<script type="text/javascript" src="../js/winkelwagen.js"></script>

<meta http-equiv="content-type"
content="text/html;charset=UTF-8" /> 
</head> 
<body> 

<div id="container">
     
<img class="header" src="../afbeeldingen/header.png"  alt="Header"/> 

<ul id="navigatie">
	<li><a href="../index.html">Home</a></li>
	<li><a href="../paginas/aanbod.html">Aanbod</a></li>
	<li><a href="../paginas/contact.html">Contact</a></li>
	<li><a href="../paginas/login.html">Login/Register</a></li>
</ul>
    <h1>Aanbod</h1> 





	<ul id="aanbod">
	<li><a href="./aanbiedingen/beautifulmind.html"><img class="1"  src="../afbeeldingen/aanbod/tumb/aBeautifulMind.jpg"  alt="abd"/></a>
	<p class="aanbod">A Beautiful Mind</p></li>
	<li><a href="./aanbiedingen/socialnetwork.html"><img class="1"  src="../afbeeldingen/aanbod/tumb/TheSocialNetwork.jpg"  alt="tsn"/></a>
	<p class="aanbod">The Social Network</p></li>
	<li><a href="./aanbiedingen/therock.html"><img class="1"  src="../afbeeldingen/aanbod/tumb/therock.jpg"  alt="tr"/></a>
	<p class="aanbod">The Rock</p></li>
	<li><a href="./aanbiedingen/inception.html"><img class="1"  src="../afbeeldingen/aanbod/tumb/inception.jpg"  alt="inc"/></a>
    <p class="aanbod">Inception</p></li>
	</ul>

	<p>   



			<img class="footer" src="../afbeeldingen/footer.png" alt="Footer"/>  
		</p>
			</div>



</body> 
</html>

Link to comment
https://forums.phpfreaks.com/topic/237212-js-function-showproducts-does-not-work/
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.