Jump to content

Loop Ajax call with data returned from previous Ajax call


vortexbased

Recommended Posts

Hello,

 

I've been trying this for hours now, looking at different examples and trying to change them to work for me, but with no luck...

 

This is what I am trying to do:

 

I have a simple form with:

- 1 input field, where I can enter a number

- 1 Submit Button

 

When I enter a number into the field and click submit, I want that number to be send to the php file that is in the ajax call, then the script will take that number and run a bunch of queries and then return a new number. I want that new number to be used to call the php script via ajax again, until no number is returned, or something else is returned like the word "done" or something like that, at which point is simply makes an alert or populated a div with a message...

 

The point is, that depending on the number entered it could take up to an hour to complete ALL the queries, so I want the script that is called to only run a fixed amount of queries at a time and then return the number it is currently at (+1), so that it can continue with the next number when it is called again.

 

I would like to use jquery, but could also be any other way, as long as I get this to work. I already have the php script completed that needs to be called by the ajax, it returns a single number when being called.

 

Thank you,

vb

Link to comment
Share on other sites

I have tried and think this is the closest I have gotten to something that could work, of course it doesn't work as it is, and I am not good enough to spot the error...

 

If someone could help me debug it, that would be awesome, though I don't mind a better or easier solution...

 

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script>
<script type="text/javascript">
	function checkForData() {
	jQuery.get('master_import.php?start=' + getElementById('starter').value,false,function(data){
			if(data >= getElementById('starter').value){
				document.getElementById('starter').value = data;
				document.getElementById('my_response').innerHTML = "Processing records... (" + data + ")";
				checkForData(data);
			}elseif(data == "done"){
				document.getElementById('my_response').innerHTML = "Done!";
			}else{
				document.getElementById('my_response').innerHTML = "meh...";
			}
		});
	}
	$(document).ready(function() {
		$('#my_button').click(function() {
			document.getElementById('my_response').innerHTML = "loading...";
		});
	});
</script>

 

this is the form parts:

<span id="my_response"></span><br /><br />
Start from: <input type="text" name="starter" id="starter" style="width:45px"><br />
<input type="submit" name="my_button" id="my_button" value="import">

Link to comment
Share on other sites

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.