[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


JavaScript Validation of Empty/Filled Shopping Cart

Posted on 2013-10-26
Medium Priority
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")

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

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

    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;
        if (ShopID != "" && ShopID != "<br/>")
            window.location = "https://www.ebuildingpermits.com/Subscribe/SubscribeEM.aspx";
            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...


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
Question by:Enflow
  • 3
  • 2
LVL 58

Accepted Solution

Gary earned 1000 total points
ID: 39603188
Add a return false here
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.

Author Comment

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
LVL 58

Expert Comment

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.

Author Comment

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

Author Closing Comment

ID: 39603289
very clear and very nice to work with GaryC123

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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…
Suggested Courses

834 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