Wait for external method to complete before finishing?


I'm making a little calendar app, in PHP and JavaScript. To populate the events div on the first page, I run this function:

function populateEvents() { $.ajax({ type: "GET", url: "populateEvents.php", success: function(data, textStatus, XMLHttpRequest) { $('#events').html(data); }, error: function(jqXHR, textStatus, errorThrown) { alert("Status: " + textStatus + ". Error thrown: " + errorThrown); } }); }

Pretty simple, populateEvents.php does most of the work.

Now, when I want to add an event, I call the following function:

function createEvent(u_id) { var title = $("#titleInput").val(); var type = $("#typeSelect").val(); var location = $("#locationInput").val(); var date = $("#inputDate").val(); var time = $("#inputTime").val(); var allday = $('#allDaySelect:checked').val() !== undefined ? '1' : '0'; var duedate = type == 'todo' ? date : ''; var notes = $("#inputNotes").val(); var output = 'u_id=' + u_id + '&title=' + title + '&type=' + type + '&location=' + location + '&date=' + date + '&time=' + time + '&allday=' + allday + '&duedate=' + duedate + '&notes=' + notes; $.ajax({ type: "GET", url: "addEvent.php", data: output, success: function(data, textStatus, XMLHttpRequest) { if (data == '') { toggleNewEvent(); populateEvents(); } else { // extract form problems from formProblems, in form: title=empty&date=empty&... var errorArray = data.split("&"); var fields = ''; $.each(errorArray, function(index, value) { var field; var smallArray = value.split("="); field = smallArray[0]; fields = fields + field + ', '; }); alert('You must fill in the following fields: ' + fields); } }, error: function(jqXHR, textStatus, errorThrown) { alert("Status: " + textStatus + ". Error thrown: " + errorThrown); } }); }

A bit more complicated, but I'm sure you get the gist. Values sent to addEvent.php, return string parsed, etc, events repopulated. Now, I just restructured the whole PHP side, to implement an eventlist class and an event class. Previously, the addevent.php file did all the actual backend work, but now, that file (after some validation) boils down to this:

if ($return == '') { /*If return is empty, everything is fine, carry on.*/ $eventlist->addEvent($u_id, $title, $type, $date, $time, $allday, $location, $dueby, $notes); } else { /*Throw an error*/ echo ($return); }

As a result however, this file returns to the JavaScript function when it's sent that request to the eventlist object to execute the addEvent method. The function then tries to populate the event div immediately, and as a result, it doesn't work, because the addEvent method hasn't finished. When you refresh the page, however, it works fine, so I know it is inserting them correctly, it just needs to wait for that second PHP file to finish.


Why not return the $eventlist->addEvent($u_id, $title, $type....... part to the script, echo $eventlist->addEvent($u_id, $title, $type........

Then in your addEvent method return something, i.e. the ID of the event.

Then instead of checking the page is blank, you are checking for an ID <strong>(</strong> <em>if (!isNaN(data)) isNotANumber</em> <strong>)</strong>, thus you know you have finished inserting your event as your page wouldn't return until it has the ID.


  • Group list of tuples by item
  • IE11 throwing “SCRIPT1014: invalid character” where all other browsers work
  • Zoom in and out of jPanel
  • Firefox Extension - Monitor refresh and change of tab
  • How to convert workspace coordinates to screen coordinates?
  • How integrated is Collada to OpenGL ES
  • Saving Changes After In-App Purchase Has Been Purchased
  • jQuery ready not fired after rails link_to is clicked
  • Debug.DrawLine not showing in the GameView
  • How to define custom class, title, and target in Link Browser for content elements and the new rte_c
  • Display images in Django
  • How to match http request and response using Jersey ContainerRequestFilter and ContainerResponseFilt
  • JQuery Internet Explorer and ajaxstop
  • Moving mysql files across servers
  • $wpdb not working in file of WordPress plugin
  • jQuery .attr() and value
  • How to use an array of arrays with array_map(…) in PHP?
  • MongoDB in PHP using aggregate to group by _id is null not working
  • Paperclip, set path outside of rails root folder
  • R - Combining Columns to String Based on Logical Match
  • Get one-time binding to work for ng-if
  • Display issues when we change from one jquery mobile page to another in firefox
  • Illegal mix of collations for operation for date/time comparison
  • Opengl-es onTouchEvents problem or a draw problem? [closed]
  • Running a C# exe file
  • Release, debug version and Authorization Google?
  • To display the title for the current loaction in map in iphone
  • Do create extension work in single-user mode in postgres?
  • R: gsub and capture
  • jqPlot EnhancedLegendRenderer plugin does not toggle series for Pie charts
  • Comma separated Values
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • trying to dynamically update Highchart column chart but series undefined
  • Hits per day in Google Big Query
  • Java static initializers and reflection
  • Android Google Maps API OnLocationChanged only called once
  • Linking SubReports Without LinkChild/LinkMaster
  • apache spark aggregate function using min value
  • UserPrincipal.Current returns apppool on IIS
  • How to load view controller without button in storyboard?