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

  • Home
  • SEARCH
  • 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 7504649
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T21:38:37+00:00 2026-05-29T21:38:37+00:00

get_ing_id gets a string, looks to the database and retrieve relevant id data according

  • 0

“get_ing_id” gets a string, looks to the database and retrieve relevant id data according to the string. At this example, it needs to retrieve ’65’ and ’66’ which it does successfully when the code is executed.

var ings = [];
var ingsSearch = [];
var i = 2;
var k = 0;
ings[0] = 'apple';
ings[1] = 'banana';

    function next(){
        for(j=0;j<i;j++){
            if(ings[j] != 0){           
                $.ajax({
                    url: burl+'index.php/ajax/get_ing_id',
                    type: 'POST',   
                    data: 'name='+ings[j],
                    success: function(data){    
                        ingsSearch[k] = data;
                        alert(ingsSearch[k]);
                        k++;
                        alert(k);
                    }
                });                     
            }

        }   
    alert(ingsSearch[0]);
    alert(ingsSearch[1]);

    }

When the code is executed, I get alert boxes in order:

  • 66
  • 1
  • 65
  • 2
  • undefined
  • undefined

It supposed to be like this

  • 66
  • 1
  • 65
  • 2
  • 66
  • 65

So where am I doing wrong? I’m not very experienced with javascript, I’ll be grateful if I get any help. Thanks for your time!

  • 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-29T21:38:39+00:00Added an answer on May 29, 2026 at 9:38 pm

    Try setting the async property of the ajax object to false. Default is true so there is no guarantee the calls will finish sequentially:

    $.ajax({
        async: false,
        // ..
    });
    
    • ajax()jQuery <– see async property

    Do note ShankarSangoli’s comment; the ideal way to code this is not loop-based, but to progress to the next element from the AJAX callback function.

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

Sidebar

Related Questions

I was using the following code to retrieve HTML snippets from a database table
I have three core data entities which I am importing into, they are: •
I want to use find_or_create_by, but this statement does NOT work. It does not
We get a large amount of data from our clients in pdf files in
When I try and run my program I get this warning and some weird
I have the following domain object: public class Data { public virtual int ID
I'm looking to create a 32-bit hash of some data objects. Since I don't
I'm looking to add a pretty simple SQLite database to an existing Java EE
I initially create a table using some data loaded from a php mySQL query.
I'm trying to get this function to work where it returns the file location

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.