Onclick issue

Hai folks,

in below code the refresh(); , an ajax function does not invoke

onclick="add_emp();[B]refresh();[/B]"

but this works

onclick="add_emp();[B]refresh();[/B][B]refresh();[/B][B]refresh();[/B][B]refresh();[/B]"

Now it works :smiley: . Strange trick is it :rofl:

So whats the problem really?

Is there a way for us to experience (and thus troubleshoot) the same problem?

Without knowing more about your particular situation, the standard technique is to pass a callback to the ajax function, so that the data on a successful request can be passed to the callback.

Hai paul,

its not possible to generate the problem simply. coz it a whole lot of coding.
in summary i can tell like this

  • form.php have a div with the id of ‘res’. also have a text box and a button. now user enters the employee number and click the button.

so add_emp() will be executed in the following order: button click > ajax call > php ad record

  • now reloading all the employee data in to the res div using refresh() : ajax call > php load data from table.

problem is , this loading does not occur if i use only 1 refresh().

oops, i fogot,

if i put an alert(); in the refresh ajax function as follows even works!!

function refresh(){
	
       alert('hai');

	var xmlhttp;
	
	if (window.XMLHttpRequest){
	  // code for IE7+, Firefox, Chrome, Opera, Safari
	  xmlhttp=new XMLHttpRequest();
	}else{
	  // code for IE6, IE5
	  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}

The load part shouldn’t occur after the ajax call, because data is still being transferred.
Instead, the load part needs to occur only when the the success event occurs for the ajax command.

Thanks paul for the valuable info.

so i think i could use the readyState==4 && xmlhttp.status==200 in the first ajax file to handle this situation …

There’s a book called Bulletproof Ajax that gives all the details on how to do that well.
They provide code and examples from the book on their website too, from which much learning can be done.

sure,ill consider about this book . thanks a lot.