Jump to content
Sign in to follow this  
neilfurry

Help with json to javascript

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

 

 

 

 

Share this post


Link to post
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);
});

Share this post


Link to post
Share on other sites

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;
});
 

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Sign in to follow this  

×
×
  • 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.