Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6160365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:17:17+00:00 2026-05-23T21:17:17+00:00

Alright, I have been breaking my head over this one for more than a

  • 0

Alright, I have been breaking my head over this one for more than a day now. And I’m sure it’s very simple to solve.

The function below is supposed to do this: get some post data from a php file (xml format returned), on success process that data to check if an item in the cart already exists with this item, to prevent it from being added twice under the same name. So it loops through the current list of items and finds a match. If it’s not found, it will add the item just fine. If it is there, a manipulation needs to take place to update that row with the new amount.

For some strange reason I can’t get the code to execute as soon as there is a match. if(match == true) just doesn’t do anything, even though console.log("index found present status: "+match); returns true in the console just fine a few lines above that.

So how is this possible? Does it have to do with scope or something?

The if(basket.fnGetData().length == 1) addToCart('window', 1); is only there to make sure the second call of the function there is a duplicate item that needs to be ‘merged’ into one. Saves me clicking on stuff on the website every time.

    function addToCart (ueid, amount)
    {
    // you can remove the follow alert() call, typically this function would
    // scroll (through an #anchor or maybe tween/scroll with jQuery) down to
    // the area below the panorama tour and there display the user-interface
    // for buying/sponsoring items

    console.log("buy "+ueid+" "+amount+" times");
    var match = false;

    console.log("before ajax present status: "+match); //output: false

    //Get detailed info about this item
    iteminfo = jQuery.ajax(iteminfourl, { data: { "ueid": ueid }, type: "POST",
    success: 
        function(data) 
        {
            console.log("ajax success present status: "+match); //output: false
            totalprice = (amount * $(data).find('price').text());

            //first check if the item already exists in the table
            currentContents = basket.fnGetData();

            if(currentContents.length > 0)
            {
                for(index = 0; index <= currentContents.length; index++)
                {
                    if(currentContents[index][0] == ueid)
                    {
                        console.log("ueid and index are the same");

                        match = true;
                        var updateIndex = index;                    
                        console.log("index found present status: "+match); //output: true
                    }
                }
            }

            if(match == true)
            {
                //this never gets executed, even if match is set to true. 
                                //also console.logs just before the if statement are only 
                                //executed when match = false all the way...
                                console.log("item is present, update index "+updateIndex);
                //basket.fnUpdate()
            }
            else
            {           
                basket.fnAddData(
                [
                    ueid,
                    amount,
                    totalprice,
                    'X'
                ]
                );

                if(basket.fnGetData().length == 1)
                    addToCart('window', 1);
            }
        }
    }); 
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T21:17:18+00:00Added an answer on May 23, 2026 at 9:17 pm
    for(index = 0; index <= currentContents.length; index++)
    

    should be

    for(var index = 0; index < currentContents.length; index++)
    

    The key being use < instead of <= in the loop expression.
    I think your loop is running an extra time and throwing a TypeError when you do currentContents[1 too many][0]. Look at the debug console for error warnings being printed out.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Alright now, I have been searching everywhere for a way to accomplish this and
Alright ive been bashing my head against the wall on this one for the
Alright, I have been messing with this for a few hours now, and I
Alright, so I have been wondering how to do this for a while. Basically,
Alright, I know questions like this have probably been asked dozens of times, but
Alright. This may be difficult but I have been struggling for quite a bit
I have been having trouble with the Google Maps API for one week now
Alright I have a question that seems simple but I haven't been able to
Alright so i have been working on this Dynamic load of a spinner from
Yo, Alright been noodling on this one for a while: How copy/cut styled text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.