here’s my code with jquery:
function check_hotel_exists(value, col)
{
var url = base_url + 'ajax/checkuniquehotel';
var response = false;
$.get(url, {hotelname:value}, function(data){
if (data === 'true') response = true;
});
alert((response) ? 'Hotel is Not Existing' : 'Hotel Exists');
return [response, "That hotel already exists"];
}
the value of response does not change.
How do I change the value of response with a callback from the get function? I am expecting the variable data from a server response which is either ‘true’ or ‘false’.
Any help would be appreciated.:)
The value does not change because the ajax callback that is supposed to set response to true executes asynchronously after the
check_hotel_existsfunction exited. A possible workaround would be to execute the ajax request synchronously by setting theasyncoption tofalse.Another workaround is to perform the call asynchronously but in this case all the work has to be done in the
successcallback, your function no longer need to return a value (for example show an error message if the hotel name already exists in the success callback). IMHO this is a better approach.