JavaScript Validation of Empty/Filled Shopping Cart

Posted on 2013-10-26
Last Modified: 2013-10-26
live web page with error:

JavaScript file...

// 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;"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 = "";
            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
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
  • 3
  • 2
LVL 58

Accepted Solution

Gary earned 250 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Fix Form size HTML 16 31
Table doesn't show the lines! 3 24
Add - in a loop to first and last element. 6 41
Scroll 5 news at a time using vticker 2 11
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.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…

726 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