I want to create a loop for input so that the variable img get number 1 to 5 like this:
img1, img2 ... img5.
How to write $i after img?
for ($i=1;$i<=5;$i++) {
function(data) { $('input[name="img1"]').val(data) });
}
Note: img is between two quotation mark.
it’s edite:
user = $('input[name="name"]').val();
for (var i = 1; i <= 5; i++) {
$.post("test.php", { name: user, num: i },
function(data) {
$('input[name="img'+i+'"]').val(data)
});
}
The function you have declared in your loop seems weird. That’s not valid javascript. You may try the following:
or if we suppose that you have defined some function:
you could invoke it like this:
UPDATE:
An interesting example was provided in the comments section:
This won’t work because the i variable might have changed value between the loop and the AJAX success callback. To fix this you may try the following:
or use the
$.ajax()method which allows you to pass a context to thesuccesscallback: