0

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!'); } }); 
6
  • Where do k and f come from, what do you think print() 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 of rad, add, latit and longit don't change inside your while loop, which happens to be in a position that makes your code invalid. Commented Aug 5, 2012 at 19:54
  • k represents 1. f represents the number that is unique to each user. It represent the total number of inputs an user puts in. For each loop, I expect the k in the variables rad, add, latit, and longit to increment by one. This will produce a list of all the key and values I want sent through in .post(). That is what I want to do, and as you said, it makes my code invalid. Commented Aug 5, 2012 at 20:08
  • You should abstract that loop into a function/method and instantiate it as needed within the $.post() success callback. Pass whatever data that abstracted method needs as an argument. Commented Aug 5, 2012 at 20:09
  • Ok, I'm starting to see what you were trying to do, but unfortunately you were way off. You're also using a lot of variables that seem to come from nowhere; such as k, f, user_id` and number_of_places. You mentioned that f is a number unique to each user, but where in your JavaScript is it defined prior to this function? k you said represents 1, so that we could just initialise easily enough. You're also expecting the value of k in your declarations for rad, etc to magically change as you loop through it; that's not how JavaScript (or any other language I've used) works. Commented Aug 5, 2012 at 20:17
  • Thanks. I guess this was a trial and error. I'm going to avoid the while loop and find an alternate route. Thanks for the help. Commented Aug 5, 2012 at 20:26

2 Answers 2

1

I don't think that would be possible, as the data parameter is an object.

What are you trying to achieve exactly?

If you are trying to keep posting user's data to a file, I'd use

// while loop here function doPost(){ $.post(url, {dynamic: dynamicdata}); } setInterval(doPost(), 100); 

...and use a while loop outside to update the parameters dynamically. If not. I'm not sure what your trying to achieve as far as an alternative goes. I hope this answer was helpful in some way.

Oh and another thing you can customise what the data would be by doing something like this

var data = {name: dynamic}; // could insert while loop to edit what date is equal to. $.post('', data); 
Sign up to request clarification or add additional context in comments.

5 Comments

I have the user input some data based on a number he chooses. The higher the number is the more input the user inserts. Thus, I want a while loop to run within the .post that reflects the total amount of user input data. After thinking about it, could I insert the fields empty by the user within the .post? Will that cause efficiency issues?
I don't think it would at all. :) Just in your php remember to check with 'if(!empty($_POST['value'])){}'
I did some adjustments to my answer.
So when the variable is written into .post(), it will automatically print out name: dynamic?
Yes. :) Remembering that in my answer dynamic was a variable that could be from a while loop or anything.
1

just by tidying up your code it appears that you are missing a } at the end closing your .post() function and a , to separate the while loop from other elements. fixed them for you

on another note, i dont think you can just send a while loop. put the loop inside a function and just pass the function and obviously you need to figure out how to pass the variables somehow :

whileFn: function() { while (/*condition*/) { // do something here } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.