I want to set up a while loop within a ajax .post jquery function. Is it even possible? If not, is there an alternate solution to the problem. The user input data is dynamic and the quantity changes for each user. That is a must. Here is the code below.
$('#send').click(function() { var rad = 'radius_' + k + ' : ' + 'radius_' + k + ', '; var add = 'address_' + k + ' : ' + 'address_' + k + ', '; var latit = 'latitude_' + k + ' : ' + 'latitude_' + k + ', '; var longit = 'longitude_' + k + ' : ' + 'longitude_' + k + ', '; $.post('ajax_set_place.php', { while (k < f) { print(rad); print(add); print(latit); print(longit); var k = k++; }, user_id: user_id, number_of_places: number_of_places }, function(){ $('#message_sent').html('Message Sent!'); } });
kandfcome from, what do you thinkprint()does in JavaScript (I'm pretty sure it doesn't do what you think it does), and what exactly is it you're trying to do? In addition, the values ofrad,add,latitandlongitdon't change inside yourwhileloop, which happens to be in a position that makes your code invalid.k,f,user_id` andnumber_of_places. You mentioned thatfis a number unique to each user, but where in your JavaScript is it defined prior to this function?kyou said represents1, so that we could just initialise easily enough. You're also expecting the value ofkin your declarations forrad, etc to magically change as you loop through it; that's not how JavaScript (or any other language I've used) works.