views:

63

answers:

1
$('.updateDescriptionProduct').click( function() {
  if ( updateProductDescription(productID, description) == 'true') {
    alert('success!');
  } else {
    alert('did not update');        
  }
});

function updateProductDescription(productID, description) {
  $.ajax({
    url: '/index.php/updateProductDescription',
    global: false,
    type: 'POST',
    data: ({productID: productID, description: description}),
    dataType: 'html',
    async:false,
    success: function(msg){
      alert(msg); 

      return msg;
    }
  });
}

The function itself says true, but my click event comes back as undefined.

+7  A: 

the return applies to the callback. Try setting a variable in the initial function and setting the value in the callback, then returning it?

function updateProductDescription(productID, description) {
    var ret = false;
    $.ajax({
          url: '/index.php/updateProductDescription',
          global: false,
          type: 'POST',
          data: ({productID: productID, description: description}),
          dataType: 'html',
          async:false,
          success: function(msg){
            ret = msg;
          }
    });
    return ret;
}

I haven't ever done an async call but it seems this should work. Let me know if it doesn't.

Gregory Baker
But he disabled it with `async:false`. Otherwise he'd have to refactor it because he's doing it asynchronously.
Gregory Baker
Yes, sorry didn't notice. +1
Daniel Vassallo
That was it! Thanks for the quick response.
Chris
+1 Very nice, `async:false`
Alexander