unemployment Posted June 30, 2011 Share Posted June 30, 2011 I'm running... but console.log(ajax_call(info)); keeps being displayed as undefined. function autosuggest_results (info){ if (info.length > 1) { var with_field = document.getElementById('users'); var with_auto_id = document.getElementById('with_auto'); var search_emp_id = document.getElementById('search_employees'); var users_list = document.getElementsByName('with_uid[]'); if(with_auto_id){ var with_auto = with_auto_id; }else{ var with_auto = search_emp_id; } ajax_call(info); console.log(ajax_call(info)); } else { hide(); } } function ajax_call(info){ var with_u_list = prev_picked(); ajax.get('/assets/ajax/associates_search.php?info=' + info + '&sid=' + hex_md5(sess_id()) + '&list=' + with_u_list, function(resp) { var search = eval('(' + resp + ')'); show(); var with_auto_id = document.getElementById('with_auto'); var search_emp_id = document.getElementById('search_employees'); if(with_auto_id){ var with_auto = with_auto_id; }else{ var with_auto = search_emp_id; } if(search.length > 0){ fill_list(search); }else{ removeChildren(with_auto); var no_res_div = document.createElement('div'); no_res_div.className = 'as_no_res'; appendText(no_res_div, 'No associates found for \'' + info + '\'. He/she may already have been added.'); with_auto.appendChild(no_res_div); } //console.log(search); return search; }); } Quote Link to comment https://forums.phpfreaks.com/topic/240767-do-i-need-a-call-back-function/ Share on other sites More sharing options...
RichardRotterdam Posted June 30, 2011 Share Posted June 30, 2011 You could do that. This is how it works the ajax request is done and starts waiting for a response. However the javascript is being run directly after the request and it's not waiting for the response to continue. You can either use a callback function to run within the ananymous function inside get() or you could do synchronous(by setting async:false) call which locks up the whole javascript execution untill you get a response. I'd go for the callback. Quote Link to comment https://forums.phpfreaks.com/topic/240767-do-i-need-a-call-back-function/#findComment-1236992 Share on other sites More sharing options...
unemployment Posted July 1, 2011 Author Share Posted July 1, 2011 You could do that. This is how it works the ajax request is done and starts waiting for a response. However the javascript is being run directly after the request and it's not waiting for the response to continue. You can either use a callback function to run within the ananymous function inside get() or you could do synchronous(by setting async:false) call which locks up the whole javascript execution untill you get a response. I'd go for the callback. Can you please show me how to do this? I have never made a call back function and reading about them has just confused me. It would be great if you could give me an example. Quote Link to comment https://forums.phpfreaks.com/topic/240767-do-i-need-a-call-back-function/#findComment-1237142 Share on other sites More sharing options...
Adam Posted July 1, 2011 Share Posted July 1, 2011 It looks like you're already using a call back to me..? ajax.get('/assets/ajax/associates_search.php?info=' + info + '&sid=' + hex_md5(sess_id()) + '&list=' + with_u_list, function(resp) { // this is your call back code... } Quote Link to comment https://forums.phpfreaks.com/topic/240767-do-i-need-a-call-back-function/#findComment-1237365 Share on other sites More sharing options...
unemployment Posted July 2, 2011 Author Share Posted July 2, 2011 That's good to know, but if that is the case, then why is my console.log returning undefined? Quote Link to comment https://forums.phpfreaks.com/topic/240767-do-i-need-a-call-back-function/#findComment-1237541 Share on other sites More sharing options...
RichardRotterdam Posted July 2, 2011 Share Posted July 2, 2011 Have you tried this? ajax.get('/assets/ajax/associates_search.php?info=' + info + '&sid=' + hex_md5(sess_id()) + '&list=' + with_u_list, function(resp) { console.log(resp); } Quote Link to comment https://forums.phpfreaks.com/topic/240767-do-i-need-a-call-back-function/#findComment-1237560 Share on other sites More sharing options...
unemployment Posted July 2, 2011 Author Share Posted July 2, 2011 Have you tried this? ajax.get('/assets/ajax/associates_search.php?info=' + info + '&sid=' + hex_md5(sess_id()) + '&list=' + with_u_list, function(resp) { console.log(resp); } Yes, that of course works. I don't think you're really understanding my problem. I have these two functions. function associate_search (){ this.autosuggest_results = autosuggest_results; function autosuggest_results (info){ if (info.length > 1) { var with_field = document.getElementById('users'); var with_auto_id = document.getElementById('with_auto'); var search_emp_id = document.getElementById('search_employees'); var users_list = document.getElementsByName('with_uid[]'); if(with_auto_id){ var with_auto = with_auto_id; }else{ var with_auto = search_emp_id; } var with_u_list = prev_picked(); ajax.get('/assets/ajax/associates_search.php?info=' + info + '&sid=' + hex_md5(sess_id()) + '&list=' + with_u_list, function(resp) { var search = eval('(' + resp + ')'); show(); var with_auto_id = document.getElementById('with_auto'); var search_emp_id = document.getElementById('search_employees'); if(with_auto_id){ var with_auto = with_auto_id; }else{ var with_auto = search_emp_id; } if(search.length > 0){ fill_list(search); }else{ removeChildren(with_auto); var no_res_div = document.createElement('div'); no_res_div.className = 'as_no_res'; appendText(no_res_div, 'No associates found for \'' + info + '\'. He/she may already have been added.'); with_auto.appendChild(no_res_div); } return search; }); } else { hide(); } } } with_field.onkeyup = function(event) { var ee = associate_search.autosuggest_results (with_field.value); console.log(ee); } I keep getting undefined in my console.log(ee) even though search is being returned in the ajax callback. Quote Link to comment https://forums.phpfreaks.com/topic/240767-do-i-need-a-call-back-function/#findComment-1237577 Share on other sites More sharing options...
RichardRotterdam Posted July 2, 2011 Share Posted July 2, 2011 I did explain why that doesn't work in the first post and how to make that work. If you want the function to work that way you will need to a synchronous, rather than asynchronous call. But I can't show you how to do that because the function ajax.get isn't shown in your post. The thing that's unclear is why you want the value to be returned? What do you need to do with that data that's retrieved using an ajax call? Quote Link to comment https://forums.phpfreaks.com/topic/240767-do-i-need-a-call-back-function/#findComment-1237643 Share on other sites More sharing options...
unemployment Posted July 2, 2011 Author Share Posted July 2, 2011 I did explain why that doesn't work in the first post and how to make that work. If you want the function to work that way you will need to a synchronous, rather than asynchronous call. But I can't show you how to do that because the function ajax.get isn't shown in your post. The thing that's unclear is why you want the value to be returned? What do you need to do with that data that's retrieved using an ajax call? I made my ajax synchronous and that still made my console.log() show up undefined. Any other ideas? Quote Link to comment https://forums.phpfreaks.com/topic/240767-do-i-need-a-call-back-function/#findComment-1237691 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.