Try doing this and see if it works:

<style type="text/css">#msg_div {font-color:red;}</style>
$.post('save_search.php', formData, function(data) {
  jsonData = eval('(' + data + ')');
  if (jsonData.return_status.search("successful") > -1) {
    $('#msg_div').html("Search was saved");
  } else {
    $('#msg_div').html("Search was not saved. Try saving again.");
  }
} );

If it works, but you want to be able to reuse the callback Fn, then do
it this way:

<style type="text/css">#msg_div {font-color:red;}</style>
function saveSearchFn(data) {
  jsonData = eval('(' + data + ')');
  if (jsonData.return_status.search("successful") > -1) {
    $('#msg_div').html("Search was saved");
  } else {
    $('#msg_div').html("Search was not saved. Try saving again.");
  }
}
$.post('save_search.php', formData, safeSearchFn);

On Aug 20, 9:34 am, dkomo872 <[email protected]> wrote:
> On Aug 20, 7:01 am, Leonard Martin <[email protected]> wrote:
>
>
>
> > The trouble is that the $.post is run asynchronously so anything after
> > the $.post will be executed before the callback function.
>
> > If you want the returned data to be available outside the callback
> > then it will have to be inside a function which is called from within
> > your callback method.
>
> > e.g.
>
> >  var data1;
>
> >     $.post('save_search.php', formData, function(data) {
>
> >          data1 = data;
> >          handle();
>
> >        } );
> > function handle() {
> >     jsonData = eval('(' + data1 + ')');
>
> >     if (jsonData.return_status.search("successful") > -1)
> >       $('#msg_div').html("<font color=red>Search was saved</font>");
> >     else
> >       $('#msg_div').html("<font color=red>Search was not saved. Try
> > saving again.</font>");
>
> > }
>
> Sadly, I talready ried this and hit what appears to be another
> problem.   "handle()" (or my equivalent function) came up as undefined
> according to Firebug when I tried to call it inside the callback
> function.
>
> It may be because my Javascript code is within the <body> of the HTML
> file (due to reasons out of my control) rather than the <head>.  Maybe
> I can't use functions this way inside <body>.  Or maybe it's same old
> asynchronous problem with Ajax.

Reply via email to