LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-21-2013, 11:50 AM   #1
ButterflyMelissa
Senior Member
 
Registered: Nov 2007
Location: Somewhere on my hard drive...
Distribution: Manjaro
Posts: 2,766
Blog Entries: 23

Rep: Reputation: 411Reputation: 411Reputation: 411Reputation: 411Reputation: 411
Exposing a feebnack value from withing an AJAX call-back function...


Hi all,

Please consider the following snippet:

Code:
<script>
	var aValue
	
	function dothis()
	{
         getID("peter","pan");
         console.log(aValue);	
        }
	
	function getID(a,b)
	{
	$.post('getID.php', {aname: a, apass: b},
	function (fbdata)
		{
			aValue = fbdata;
		}
	);				
	}
</script>
The JS function doThis() is called upon the load from a <body> tag, it in turn calls the getID() function, and AJAX call to a PHP script on the server. Though the variable aValue is declared globally, it comes back as undefined. How do I expose the results from the call-back function un the $.post call?
PLease consider I'm learningt he ropes here...
Thank you for a reply...
Thor
 
Old 11-21-2013, 12:13 PM   #2
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 447Reputation: 447Reputation: 447Reputation: 447Reputation: 447
Are you sure the ajax call doesn't fail? If it does, your function will not be called. If you add alert(fbdata) or console.log(fbdata) what is it?
 
Old 11-21-2013, 12:26 PM   #3
ButterflyMelissa
Senior Member
 
Registered: Nov 2007
Location: Somewhere on my hard drive...
Distribution: Manjaro
Posts: 2,766

Original Poster
Blog Entries: 23

Rep: Reputation: 411Reputation: 411Reputation: 411Reputation: 411Reputation: 411
if I do a condole feeb dicectly in the caal-back I get the ID...

Code:
<script>
	var aValue
	
	function dothis()
	{
         getID("peter","pan");
         console.log(aValue);	
        }
	
	function getID(a,b)
	{
	$.post('getID.php', {aname: a, apass: b},
	function (fbdata)
		{
			aValue = fbdata;
                        console.log(aValue);
		}
	);				
	}
</script>
If I do a feedback on the hi-lited spot, I get the info back, beyond the call-back, it's lost ergo "undefined" - the PHP does work...

Thanks for helping out

Thor

Edit - the result is the same even if the variable is declared in an included file...

Last edited by ButterflyMelissa; 11-21-2013 at 12:35 PM.
 
Old 11-22-2013, 05:49 AM   #4
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 447Reputation: 447Reputation: 447Reputation: 447Reputation: 447
Then I guess it's because ajax calls are asyncronous. In your second example, do you get "undefined" in the console before the actual value? The function getID returns first, and your anonymous function is called when the response from the ajax call is ready.

You can change it to be syncronous instead.

http://stackoverflow.com/questions/1...ronous-ajax-re
 
1 members found this post helpful.
Old 11-24-2013, 03:16 PM   #5
ButterflyMelissa
Senior Member
 
Registered: Nov 2007
Location: Somewhere on my hard drive...
Distribution: Manjaro
Posts: 2,766

Original Poster
Blog Entries: 23

Rep: Reputation: 411Reputation: 411Reputation: 411Reputation: 411Reputation: 411
Thanks for the input! In act, the whole thing does work...just...I dont know how to us it - the call is made, and the PHP works in the background, in the mean time, the JavaScript moves on, extracts the value that is yet to arrive from the server,, but before it does...it's as yet undefined. As suggested, setting the call to syncronous (async:false) solves this.
Of course, this is not what AJAX is meant for, but at this critical stage, the info is needed and the call has to halt there to wait for the info to come through...
Marking this as solved
Thor
 
  


Reply

Tags
ajax, javascript



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to register user space call back function with USB driver in Linux? psmurthy Linux - Kernel 3 09-04-2011 10:56 AM
Activate V-Sat back up withing 4 seconds rudra_sakti Linux - Newbie 0 10-25-2008 06:18 AM
LXer: Solid Ajax applications, Part 2: Building Ajax back ends LXer Syndicated Linux News 0 01-22-2008 12:30 PM
Call external prog from withing chroot jail The_JinJ Linux - General 2 12-13-2005 04:38 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:17 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration