Firing Order Question
-
I am working on a plugin that allows members to register for an event. Most of it is working, however I think I have an issue with the sequence of how various portions are executed. I have an external PHP file that retrieves the registrants from a SQL table and displays them as a table. Each table row has a cancel button created with a unique ID to allow members to cancel their registration. When I include that file in an ajax callback function, it displays properly, but the cancel buttons do not do anything. If I ‘inspect element’ on the buttons the console log displays the correct ID for the cancel button, but it does not do anything. I suspect that the method I am using to display the registrant list with cancel buttons is affecting how they are processed (or not processed) by the JS file.
Here is the basic sequence of the code that I have:
At the top of the main plugin PHP file, I enqueue the JS file and define the Ajax URL.
This is followed by the main plugin function which creates some HTML input fields and RSVP button. At the bottom of this function I define a div ID to be filled in by the ajax the callback function.return '<div id="ajax_response"></div>'; }; // end of main rsvp_event_rsvp function
This is followed by 2 callback functions, one to retrieve the eventid from the page being viewed, and another to extract the user actions to RSVP for the event or cancel their reservation.
I then create a short code for the main plugin function. The short code is appended to the bottom of the page being viewed.In the JS file, the first thing that happens, is the eventid is constructed from information on the page being viewed and an ajax call is created to the eventid_callback function below. The JS ajax call is as follows:
var event_category = ($jq( ".tribe-events-event-categories" ).text()); // alert ("Event Category is " + event_category); var event_start = ($jq( ".tribe-events-start-date" ).attr("title")); // alert ("Event Start Date is " + event_start); var event_id = event_category.substring(0,1) + event_start.substring(0,4) + event_start.substring(5,7) + event_start.substring(8,10); alert ("Event ID is " + event_id); $jq.ajax({ url : ajax_rsvp.ajax_url, type : 'post', data : { action: 'eventid_callback', event_id_name : event_id }, success:function(data) { // This outputs the result of the ajax request console.log(data); // Return response to client side $jq('#ajax_response').html( data ); return false; }, error: function(errorThrown){ console.log(errorThrown); } }); // End of AJAX function
Note the jquery command which fills in the div id=ajax_response defined in the main plugin function.
The JS file then includes commands that process the various button clicks (RSVP or Cancel) and the ajax calls to the rsvp_callback function.add_action( 'wp_ajax_eventid_callback', 'eventid_callback' ); function eventid_callback() { $event_id = $_POST[event_id_name]; echo "<p>Event ID from eventid_callback is: {$event_id} </p>"; include ('event-rsvp-list.php'); rsvp_event_list($event_id); die(); }; // end of eventid_callback function
This works properly, but the buttons defined in the event-rsvp-list.php file are not processed by the JS file commands.
If I add the event list file into the main plugin php file, the registrant list is displayed properly and all buttons work so long as a I hardcode the eventid. The problem I have with this approach is that I don’t seem to be able to pass the eventid variable into the main plugin function that was processed by the eventid_callback function. I have tried making $event_id a global variable, and also trying to retrieve it from the ajax URL again with another $_POST command. Neither seemed to work. So far the only way I seem to be able to display the registrant list and have working cancel buttons is to hardcode the $event_id into the main plugin function. Anytime I display the list via the div element filled out from the ajax call, the buttons do not work.
I realize this was lengthy and confusing, but just writing down and trying to explain the issue helped me. I would appreciate any thoughts that anyone might have.
Thanks
- The topic ‘Firing Order Question’ is closed to new replies.