I have an API call that I'm using to get data. This data is an array of arrays and I need to loop through them. For each value, I want to 'load' a php template I created with those values. The very first loop works and displays properly, but after the initial value, it stops. I need all values to be looped through and displated.
Example of what is happening: [a, b, c] => JQuery loop => load => Only 'a' shows up.
EDIT - Every time load is called, it loads the script into 'elementToLoadPhpFile'. I noticed that it seems to be overwriting the previous values that are loaded there. I need it to append multiple children, not override the same child.
$.ajax({
type: 'GET',
dataType: "json",
url: 'apiUrl.com'
}).done(function(data) {
var result = data['data'];
jQuery.each(result, function(key, value) {
var id = value['id'];
var title = value['title'];
var img_url = value['images']['jpg']['image_url']
$("#elementToLoadPhpFile").load("phpFile.php", {
id: id,
title: title,
img_url: img_url
});
});
});
console.log(id, title, img_url)inside your loop to ensure the loop is working properly and retrieving the correct arguments to pass to the PHP script? Have you confirmed the PHP script is working and properly parsing its arguments?.load()inside.each()that means any URL loaded using ajax.load()will be overwrite contents into#elementToLoadPhpFileelement. See the document. To append, I recommend to use something else such as.insertAdjacentHTML().load()function is asynchronous by nature, meaning the different calls you're making will finish in an indeterminate order. Does this answer your question? jQuery .load(function) synchronous.load()will replace, which will not work.