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 9218243
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:49:04+00:00 2026-06-18T02:49:04+00:00

Okay, so I’ve been struggling through this problem for a while now and I’ve

  • 0

Okay, so I’ve been struggling through this problem for a while now and I’ve hit a wall I can’t figure out. I have a page with many forms that a user would insert different types of input which gets submitted to a mySQL database through the below ajax script (this part all works).

Right now, I need certain fields to populate with information that the user has input in other input fields. These fields in HTML are in the form:

<div class="popField" id="client_input_3_0_1_a"><?php echoInput('client_input_3_0_1_a') ?></div>
<div class="popField" id="client_input_3_0_2_a"><?php echoInput('client_input_3_0_2_a') ?></div>

(The echoInput() above is just a function that would load and populate this field when the page loads from the mySQL database, that part works). Here the id matches the id of the user input and its location in the database.

What’s not working is when the user changes some input, this should re-populate one of the fields with their change. Instead of doing this, it seems to only repopulate for the last div to be repopulated on the page, as if the data is fetched in the proper order, but it is only storing in in the last div on the page instead of each respective div. Any thoughts on where I am going wrong?

    // Retrieve all forms of '.formAjax' on the page onChange
    $('.formAjax').change(function () {
        $.post('process.php', $(this).serialize(), function(data) {
            //process.php stores input to mySQL database
        });


        //retrieve divs that need to be populated with input
        var divstopop = document.getElementsByClassName("popField");
        for (var i = 0, n = divstopop.length; i < n; ++i){
            var dbID = divstopop[i].id;
            divstopop[i].innerHTML= divstopop[i].id; //this prints each respective ID correctly
            $.post("populateField.php", {dbID: dbID}, function(returned_data){
                //here populateField.php just retreives data from the database for the id dbID and echoes it back
                //divstopop[i].innerHTML = returned_data; //Does not work at all(?)
                document.getElementById(dbID).innerHTML = dbID + returned_data; 
                //Last div to populated is the only one that changes 
                //and it flashes through what each of the other ones should be populated with
            });
        }

    });
   });

Apologies if I did not make anything clear or if I am missing something very obvious here, my brain is very tired.

  • 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-06-18T02:49:06+00:00Added an answer on June 18, 2026 at 2:49 am

    You make multiple calls with the same variable. dbID and i are updated while being used by the previous calls. That’s why only the last field is updated, because i and dbID are modified until the last iteration.

    You can fix this by creating a new scope

    function populateDiv(div) {
        var dbID = div.id;
        div.innerHTML = div.id; //this prints each respective ID correctly
        $.post("populateField.php", {dbID: dbID}, function(returned_data){
            //here populateField.php just retrieves data from the database for the id dbID and echoes it back
            div.innerHTML = returned_data;
        });
    }
    
    $('.formAjax').change(function () {
        ...
        for (var i = 0, n = divstopop.length; i < n; ++i)
            populateDiv(divstopop[i]);
        ...
    });
    

    As an aside, this is very inefficient. You make n calls to update your fields. You should think about making one call returning the data for all fields and then update the divs from that. This will reduce network traffic as well as the load on your server.

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

Sidebar

Related Questions

Okay so I can't figure this out. Like a file that I using grep
Okay so I am trying to figure out the best design for this problem.
Okay, Now admittedly this sounds like a silly question; But, I actually have a
Okay I've been trying to research how to do this and have failed. I
Okay i've been fighting this system for about 3 days now and googling left
Okay i have this problem with every page i make. im not sure what
Okay, so generally I wouldn't have a problem doing this and it would be
Okay the answer to this may be really simple but I have been searching
Okay, next PHPExcel question. I have an HTML form that users fill out and
Okay this question is very simple: I have a facebook page, and a website.

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.