Jump to content

Help with json to javascript


neilfurry
Go to solution Solved by requinix,

Recommended Posts

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

 

 

 

 

Link to comment
Share on other sites

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);
});
Link to comment
Share on other sites

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

Link to comment
Share on other sites

  • Solution

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.

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.