Solved

Javascript variable scope

Posted on 2011-03-23
8
304 Views
Last Modified: 2012-05-11
Hello, I have this function.
function save_to_db(item)
    {
        var widget_id;
        widget_name = item.attr('id'); //ie - image_widget
        alias = widget_name.replace("_widget", "");

        $.ajax({
         //ajaxsetup ftw
          data: {
              'action': 'add_element',
              'page_id': '106034386126175',
              'element_id' : alias
          },
          success: function(msg)
                   {
                       widget_id = msg;
                   }
        });

        return widget_id;
}

Open in new window


I want this function to return the data which comes from the ajax success, however it's always returning "undefined" and the ajax success works correctly. Any idea?
0
Comment
Question by:sahanz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 56

Expert Comment

by:HainKurt
ID: 35204131
try

success: function(msg)
                   {
                       widget_id = msg; alert(widget_id);
                   }

what does it show?
0
 
LVL 40

Expert Comment

by:gurvinder372
ID: 35204135
it cannot come from success handler, since success handler is a call back handler which is called asynchronously.

what you need to do is, that the method which needs the widget id needs to get called from the success handler.
0
 
LVL 56

Expert Comment

by:HainKurt
ID: 35204137
I guess you should use async=false

async: false
$.ajax({
         //ajaxsetup ftw
          async: false,
          data: {
              'action': 'add_element',
              'page_id': '106034386126175',
              'element_id' : alias
          },
          success: function(msg)
                   {
                       widget_id = msg;
                   }
        });

Open in new window

0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 1

Author Comment

by:sahanz
ID: 35204140
@HainKurt:

Works fine, widget_id alerts the correct value.
0
 
LVL 56

Expert Comment

by:HainKurt
ID: 35204175
I guess you should change your logic a bit :) if you do not want to use async
when it succeded, call another function to do the remaining jobs (if any)

success: function(msg)
                   {
                       widget_id = msg; fnDataSaved(msg);
                   }

but did you try async? it may be what you want...
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 35204336
we need to add : .responseText;

var widget_id = $.ajax({ url: "some.php", async: false }).responseText;

so :


function save_to_db(item) {
   widget_name = item.attr('id'); //ie - image_widget
   alias = widget_name.replace("_widget", "")
   var d = data: { 'action': 'add_element', 'page_id': '106034386126175', 'element_id' : alias }
   return $.ajax({ url: "some.php", async: false }).responseText;
}

Open in new window

0
 
LVL 82

Expert Comment

by:leakim971
ID: 35204338
oups I forget to add the data line 5 :


return $.ajax({ url: "some.php", data:d, async: false }).responseText;

Open in new window

0
 
LVL 1

Author Closing Comment

by:sahanz
ID: 35205350
Cool, that's what I wanted :)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

628 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question