neilfurry Posted February 7, 2016 Share Posted February 7, 2016 Hello Mate, I would like to seek help... i have this json script which gets the names of the users from my database: $sql=mysql_query("SELECT firstname FROM tblusers WHERE usertype=1"); $u = array(); while($rs=mysql_fetch_array($sql)){ $u[] = $rs['firstname']; } print json_encode($u); And getting this to javascript function through ajax: function uList(){ $.ajax({ type: "POST", url: "techs.php", success: function(data) { console.log(data); return data; } }); } my console.log shows the correct format i want to get, but it doesn't display on my calendar page.... ["user1","user2","user3","user4","user5"]... im using this code to send data to my calendar script. users:uList, Any help is very much appreciated. Thank you Quote Link to comment Share on other sites More sharing options...
requinix Posted February 7, 2016 Share Posted February 7, 2016 You cannot return the list from the uList function. AJAX is asynchronous (it's the first 'A') which means it's not executing like a normal function call but will execute eventually and somewhere else. Instead of calling uList() and then doing something with the return value, give uList() a function to call when it's ready. function uList(callback) { $.ajax({ type: "POST", url: "techs.php", success: callback }); } uList(function(users) { // do something with users console.log(users); }); Quote Link to comment Share on other sites More sharing options...
neilfurry Posted February 7, 2016 Author Share Posted February 7, 2016 how can pass this to this variable users: so if i use users:uList, would be right? thank you Quote Link to comment Share on other sites More sharing options...
neilfurry Posted February 7, 2016 Author Share Posted February 7, 2016 is this correct if i want to get the list of users and display it somewhere else function uList(callback) { $.ajax({ type: "POST", url: "techs.php", success: callback }); }uList(function(users) { return users;}); Quote Link to comment Share on other sites More sharing options...
neilfurry Posted February 7, 2016 Author Share Posted February 7, 2016 is this correct users:uList(function(callback) { return callback; }), when i use alert on callback it works as it should be working... but when i use return it displays nothing.. how can i display the content of that callback to the html screen... im attenpting to display this list of users on a calendar. thank you Neil Quote Link to comment Share on other sites More sharing options...
neilfurry Posted February 7, 2016 Author Share Posted February 7, 2016 this is where i intend to use this function... if you look at line 197, that should be the exact location where i want to display the list of users from that function im working. https://github.com/themouette/jquery-week-calendar/blob/master/weekcalendar_demo_3.html any help would be greatly appreciated. Thank you Neil Quote Link to comment Share on other sites More sharing options...
Solution requinix Posted February 7, 2016 Solution Share Posted February 7, 2016 No, you cannot return from the function. Just like you cannot use return in your original code. Unfortunately I can't find anything good that explains how AJAX works and what you can and cannot do with it. Unfortunate because I wanted to give you something to read (preferably something that would take more than a couple minutes) before you touched any more code. The callback function has to do a thing. It cannot return a thing. Use it to perform an action. Put the code performing an action inside the callback. 1. Why are you even using AJAX for this? Do you expect the list of users to change while the page is still open? What was wrong with the json_encode() from earlier? 2. The "callback function has to do a thing" approach will not work for that code. It's already within an event handler. Probably the most appropriate approach would be to set a variable (like but not quite how the other variables around lines 62+ are set) containing the list of users, and put that variable on line 197. But to do this best requires an answer to my first question. Quote Link to comment Share on other sites More sharing options...
neilfurry Posted February 7, 2016 Author Share Posted February 7, 2016 but is there anyway i can put all the users into the calendar using javascript from json file? thank you for answering my questions... i really appreciate it. Quote Link to comment Share on other sites More sharing options...
neilfurry Posted February 7, 2016 Author Share Posted February 7, 2016 i solved it just using json... Thank you mate... Quote Link to comment 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.