Jump to content

Recommended Posts

Hello,

( I'm dutch so my englisch is poor sorry :'( )

I've got a Ajax script with a for loop.
I want to make a new request from inside the for loop with a created onclick

as result i get this

id 1
id 2
id 3

i give this id with my onlick as you can see in my code below.
but if i click on the link in my result table te output in the next function handle_fact is always 3.

my code:

for loop:
[code]
for ( i=0;i<results.length; i++){
         
          var artId = result.getElementsByTagName("artId");
          var artId = artId.item(i);
          var artId = artId.firstChild.nodeValue
         
          var artTr = document.createElement("tr");
         
          var artaddTd = document.createElement("td");
          artaddTd.className = "addTd";
          artaddTd.innerHTML = artId;
         
          artaddTd.onclick = function()
          {
              make_request('mandje.php?id=' + artId, 'handle_fact', 'GET', null, true);
          }
         
          artTr.appendChild(artaddTd);
          artTbody.appendChild(artTr);
          artTable.appendChild(artTbody);
          artDiv.appendChild(artTable);
          suggestionDiv.appendChild(artTable);
       
        }
[/code]

result code handle_fact:
[code]
function handle_fact(result) {
    var productenDiv = document.getElementById("productenDiv");
     
    var results = result.getElementsByTagName('result');
    productenDiv.innerHTML = '';
     
    for( ii=0; ii<results.length; ii++) {
    var test= result.getElementsByTagName("test");
    var test = test.item(ii);
      productenDiv.innerHTML = test.firstChild.nodeValue;
    }
    }
[/code]
Link to comment
https://forums.phpfreaks.com/topic/34871-function-form-for-loop/
Share on other sites

That's certainly going to break!
  You can't make requests that fast. Each one is going to listen for a ready state.

If you had 1000 items, you'd make 1000 server requests in 10ms!
Of course, it'll break by the second one.

You should find a way to preallocate those values.
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.