Method is executed twice, but it shows one
-
Hi!
So I have this really strange problem… Or hidden feature. I created a plugin for event registration. I has a list of events, a form to register for an event, and Brevo implementation for sending emails. Everything works just perfect on my local environment.
However… When I use the plugin on my own website, the (sql) query for inserting the registration is executed twice. The sending of a confirmation e-mail, which is in the same function, is also send twice. The registration gets a GUID for activation and that GUID is not the same for both registrations.
When I do a simple echo in the function where the registration is inserted in the database and sends the e-mail, I only see one echo printed in my page. For me, this is magic.
This is a part of the function and class that handles the registration:
if (!class_exists('Shortcodes_EventRegistration')) { ? ? class Shortcodes_EventRegistration ? ? { ? ? ? ? private $event; ? ? ? ? private $stored = false; ? ? ? ? function __construct($frmid) ? ? ? ? { ? ? ? ? ? ? if(!isset($_GET["eid"])){ ? ? ? ? ? ? ? ? echo "Event key not found."; ? ? ? ? ? ? ? ? return; ? ? ? ? ? ? } ? ? ? ? ? ? global $wpdb; ? ? ? ? ? ? $query = "select eventdate.*, events.name, tz.name as timezone from {$wpdb->prefix}klc_events_dates as eventdate ? ? ? ? ? ? ? ? ? ? ? ? inner join {$wpdb->prefix}klc_events as events on eventdate.eventId = events.id ? ? ? ? ? ? ? ? ? ? ? ? inner join {$wpdb->prefix}klc_events_timezones as tz on tz.id = eventdate.timezoneId ? ? ? ? ? ? ? ? ? ? ? ? where eventdate.id = {$_GET["eid"]};"; ? ? ? ? ? ? $this->event = $wpdb->get_row($query); ? ? ? ? ? ? if (isset($_POST["formaction"]) && $_POST["formaction"] = "send_eventregistration") { ? ? ? ? ? ? ? ? echo "<div>Storing and sending<div>"; // This is shown once, but executed twice ? ? ? ? ? ? ? ? $fullName = $_POST["reg"]; ? ? ? ? ? ? ? ? $activationKey = $this->GUID(); // I disabled this, because it keeps sending emails (twice) ? ? ? ? ? ? ? ? // $emailClass = new My_Email(); ? ? ? ? ? ? ? ? // $emailClass->sendEventRegistationConfirmation($this->event, $fullName["fields"][1], $fullName["fields"][0], $activationKey); ? ? ? ? ? ? ? ? $query2 = $wpdb->prepare( ? ? ? ? ? ? ? ? ? ? "INSERT INTO {$wpdb->prefix}klc_events_registrations (eventdateid, fullname, country, email, experience, activationkey, activated) ? ? ? ? ? ? ? ? values (%d, %s, %s, %s, %d, %s, %s)", ? ? ? ? ? ? ? ? ? ? $_POST["eventdateid"], ? ? ? ? ? ? ? ? ? ? $fullName["fields"][0], ? ? ? ? ? ? ? ? ? ? $fullName["fields"][6], ? ? ? ? ? ? ? ? ? ? $fullName["fields"][1], ? ? ? ? ? ? ? ? ? ? $fullName["fields"][2], ? ? ? ? ? ? ? ? ? ? $activationKey, // Activation key is a GUID and always unique. Also for both registrations ? ? ? ? ? ? ? ? ? ? 0 ? ? ? ? ? ? ? ? ); ? ? ? ? ? ? ? ? $wpdb->query($query2); ? ? ? ? ? ? ? ? echo "<div>" . $query2 . "</div>"; // Query is shown once, but executed twice ? ? ? ? ? ? ? ? $this->stored = true; ? ? ? ? ? ? } ? ? ? ? ? ? if (!$this->stored) { ? ? ? ? ? ? ? ? $this->generateHTML(); ? ? ? ? ? ? } else { ? ? ? ? ? ? ? ? $this->registrationSend(); ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? function GUID() ? ? ? ? { ? ? ? ? ? ? if (function_exists('com_create_guid') === true) { ? ? ? ? ? ? ? ? return trim(com_create_guid(), '{}'); ? ? ? ? ? ? } ? ? ? ? ? ? return sprintf('%04X%04X-%04X-%04X-%04X-%04X%04X%04X', mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(16384, 20479), mt_rand(32768, 49151), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535)); ? ? ? ? } // Other code is HTML to show the form or a message that the registration is stored.
And this is part of the HTML form:
<form method="POST"> <input type="hidden" value="send_eventregistration" name="formaction" /> <input type="hidden" value="<?= $_GET[" eid"] ?>" name="eventdateid" /> <div id="wpforms-16958-field_0-container" class="wpforms-field wpforms-field-name" data-field-id="0"> <input type="text" id="reg-field_0" name="reg[fields][0]" placeholder="Enter your full name" required=""> <input type="email" id="reg-field_1" name="reg[fields][1]" placeholder="Enter your email address" required=""> <div> <select id="reg-field_6" name="reg[fields][6]" aria-invalid="false" placeholder="Your country"> </select> </div> <div class="experience"> <p> Which of the following description would describe your experience with C#? </p> <p> <input type="radio" id="reg_field_2" name="reg[fields][2]" value="1"> <label for="reg_field_2">"It has something to do with computer"</label> </p> <p> <input type="radio" id="reg_field_3" name="reg[fields][2]" value="2"> <label for="reg_field_3">"I know how to print 'hello world'"</label> </p> <p> <input type="radio" id="reg_field_5" name="reg[fields][2]" value="4"> <label for="reg_field_5">"I am a professional with > 10 years experience"</label> </p> </div> <div class="button"> <?php $button_html = get_submit_button('Register', 'primary', 'submit_button_name', false); echo $button_html; ?> </div> </div> </form>
The form and POST is set by a shortcode:
function eventregistration_shortcode($atts) { if (is_single() || is_page()) { $default = array( 'frmid' => 'a' ); $a = shortcode_atts($default, $atts); ob_start(); new Shortcodes_EventRegistration($a['frmid']); $output_string = ob_get_contents(); ob_end_clean(); return $output_string; } else { return "not single"; } } add_shortcode('events_registration', 'eventregistration_shortcode');
Summary:
- When a visitor registers for an event on the LIVE website, the information is stored TWICE in the database and e-mails (when activated) are send twice too.
- Placing “echo’s” in the function that stores the information are only shown ONCE
- The short code is placed once in the page (happened once, not making that mistake again)
- This works perfectly fine on LOCAL website (wamp server)
What am I doing wrong?
- The topic ‘Method is executed twice, but it shows one’ is closed to new replies.