Solved

JavaScript Validation of Empty/Filled Shopping Cart

Posted on 2013-10-26
5
357 Views
Last Modified: 2013-10-26
live web page with error: http://ebuildingpermits.com/California/Alameda-County-California-Building-Permit-Leads-Orig.aspx

JavaScript file... www.ebuildingpermits.com/subscribe/ccabpl2.js

// info passed by Refresh User Info
var xmlHttp;

function GetXmlHttpObject() {
    var objXMLHttp = null;
    // code for Mozilla, etc.          
    if (window.XMLHttpRequest) {
        objXMLHttp = new XMLHttpRequest();
    }
    // code for IE
    else if (window.ActiveXObject) {
        try {
            objXMLHttp = new ActiveXObject("Microsoft.XMLHTTP")
        }
        catch (e) {
            objXMLHttp = new ActiveXObject("Msxml.XMLHTTP")
        }
        //objXMLHttp = new ActiveXObject("Microsoft.XMLHTTP"); // Msxml  
    }
    return objXMLHttp;
}

function sendRequest(params, handler) {
    xmlHttp = GetXmlHttpObject();
    if (xmlHttp == null) {
        alert("Sorry - But Your Web Browser Does Not Support Our Shopping Cart - Please Call Us: 831-649-4659 To Order")
        return
    }

    xmlHttp.onreadystatechange = handler;
    xmlHttp.open("POST", document.forms[0].action)
    xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xmlHttp.send(params);
}

function testvalue() {
    var mytext = document.ShopForm.AreaSelected.options[document.ShopForm.AreaSelected.selectedIndex].text;
    var myvalue = document.getElementById("AreaSelected").value;
    //alert(myvalue)

    if (mytext == "Please Select A Subscription Type") {
        alert("You Forgot To Select A Subscription Type From The Drop Down Select Box");
        return false;
    }
    else {

        var params = "value=" + myvalue + "&id=" + Math.random();
        sendRequest(params, testvalueStateChanged);

        return false;
    }
}

function testvalueStateChanged() {
    if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
        if (xmlHttp.responseText != "") {
            alert("Your New Subscription has been added to your Shopping Cart...");
        }
    }
}

function checkout() {

    var params = "checkout=1";
    sendRequest(params, checkoutStateChanged);

    return false;
}

function checkoutStateChanged() {
    if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
        var ShopID = xmlHttp.responseText;
        //alert(ShopID)
        if (ShopID != "" && ShopID != "<br/>")
            window.location = "https://www.ebuildingpermits.com/Subscribe/SubscribeEM.aspx";
        else
            alert("Checkout - Why? - No Subscription Items Are In Your Shopping Cart...");
    }
}


The checkout function is NOT working right... It is supposed to test whether the database has an order in it or not (Shopping Cart is Filled or Empty)

The checkout() Function is called when a user clicks on the Proceed To Checkout button

<asp:Button ID="ProceedToCheckout" onClientClick="checkout()" runat="server" style="background-image: url(../Pics/Gifs/CheckOut.gif); background-repeat: no-repeat;" BackColor="Transparent" BorderStyle="None" ForeColor="Transparent" Height="42px" Width="215px" />

the Proceed to Checkout button is on this live page...

http://ebuildingpermits.com/California/Alameda-County-California-Building-Permit-Leads-Orig.aspx

and a JavaScript Alert pops up for a split second with the text "Checkout - Why? - No Subscription Items Are In Your Shopping Cart..."

WHETHER OR NOT THE CART IS FILLED OR EMPTY and then vanishes... which its not supposed to do...

I am confused as to how this JavaScript tests for an order in the database using the xmlHttp method so if i am a wee bit confusing or off the mark on my explanation please understand...

this WAS WORKING for years but now that the https SSL page gets a different Session ID then the regular non https SSL pages so this no longer works... i think or think incorrectly

I need help fixing this or putting in new JS code that tests for a order in the database exsits before the user will be allowed to go to the checkout page... or not allowed to go to the checkout page and an alert pops up telling them their is nothing in their cart... as i have

thanks... Cj
0
Comment
Question by:Enflow
  • 3
  • 2
5 Comments
 
LVL 58

Accepted Solution

by:
Gary earned 250 total points
ID: 39603188
Add a return false here
...
 else
alert("Checkout - Why? - No Subscription Items Are In Your Shopping Cart...");
return false;
}


In the onclick even add return
onclick=" return checkout();"

That will fix the js alert problem


Where is the add to cart request supposed to be going - at the moment it is just calling the same page you are on, you will see in the response you get the html for the current page.
0
 

Author Comment

by:Enflow
ID: 39603208
Hi Gary... :o)

Mama Mia... worked right out the gate... !

Super Duper... NOW... a minor issue... ?

I can understand we had the syntax wrong and should have had return false in the right areas... ok.... that was perfect fix... but...

But HOW DOES THIS CHECK that their is a item in the shopping cart OR NOT... ???

How is it doing that ??

cause it is... check it for your self...

add no subscription and try to go to checkout and you correctly get the Why ? alert...

but when you DO ADD a SUB then it allows you into the checkout page... how does it know there is or is not an item in the cart.. .?

you can even DELETE the item from the shopping cart on the checkout page and go back button and then again click on the Proceed to Checkout button and correctly GET the Why ? alert...

so ?

thanks... Cj
0
 
LVL 58

Expert Comment

by:Gary
ID: 39603243
Did you not write the code?
When you click the checkout button it does a post request, the response contains anything then it allows you to proceed, if the response is empty then it stops you going to the checkout page.
0
 

Author Comment

by:Enflow
ID: 39603288
We wrote all of aspx code and a bit of the JS code... but not the request/response checkout code...

thanks for the speed and clarity... awarded full points... to you... CJ
0
 

Author Closing Comment

by:Enflow
ID: 39603289
very clear and very nice to work with GaryC123
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now