Convert post-back to PHP via json to Ajax post

I am trying to integrate a Codeigniter application into WordPress through a plugin. The codeigniter program is having difficulty in the control at this point when the customer confirms the data of a new appointment.

VIEW

Read More
            <div id="wizard-frame-4" class="wizard-frame" style="display:none;">
                <div class="frame-container">
                    <h3 class="frame-title"><?php echo $this->lang->line('step_four_title'); ?></h3>
                    <div class="frame-content row">
                        <div id="appointment-details" class="col-md-6"></div>
                        <div id="customer-details" class="col-md-6"></div>
                    </div>
                </div>
                <div class="command-buttons">
                    <button type="button" id="button-back-4" class="btn button-back" 
                            data-step_index="4"><i class="icon-backward"></i> 
                        <?php echo $this->lang->line('back'); ?>
                    </button>
                    <form id="book-appointment-form" style="display:inline-block" method="post">
                        <button id="book-appointment-submit" type="button" value="saveAppointment" name="submit1" 
                        class="btn btn-success" onclick = "this.style.visibility='hidden', loading.style.visibility='visible'">
                            <i class="icon-ok icon-white"></i>
                            <?php
                                echo (!$manage_mode) ? $this->lang->line('confirm')
                                        : $this->lang->line('update');
                            ?>
                        </button>
                        <input type="hidden" name="csrfToken" />
                        <input type="hidden" name="post_data" />
                    </form>
                </div>
            </div>

JS:

    $('#book-appointment-submit').click(function(event) {
        var formData = jQuery.parseJSON($('input[name="post_data"]').val());

        var postData = {
            'csrfToken': GlobalVariables.csrfToken,
            'id_users_provider': formData['appointment']['id_users_provider'],
            'id_services': formData['appointment']['id_services'],
            'start_datetime': formData['appointment']['start_datetime'],
        };

        if (GlobalVariables.manageMode) {
            postData.exclude_appointment_id = GlobalVariables.appointmentData.id;
        }

        var postUrl = GlobalVariables.baseUrl + '/index.php/appointments/ajax_check_datetime_availability';

        $.post(postUrl, postData, function(response) {
            ////////////////////////////////////////////////////////////////////////
            console.log('Check Date/Time Availability Post Response :', response);
            ////////////////////////////////////////////////////////////////////////

            if (response.exceptions) {
                response.exceptions = GeneralFunctions.parseExceptions(response.exceptions);
                GeneralFunctions.displayMessageBox('Unexpected Issues', 'Unfortunately '
                        + 'the check appointment time availability could not be completed. '
                        + 'The following issues occurred:');
                $('#message_box').append(GeneralFunctions.exceptionsToHtml(response.exceptions));
                return false;
            } 

            if (response === true) {
                $('#book-appointment-form').submit();
            } else {
                GeneralFunctions.displayMessageBox('Appointment Hour Taken', 'Unfortunately '
                        + 'the selected appointment hour is not available anymore. Please select '
                        + 'another hour.');
                FrontendBook.getAvailableHours($('#select-date').val());
                                alert('#select-date');
            }
        }, 'json');
    });

CONTROLLER:

        if($this->input->post('submit2'))
        {       
            $post_waiting = json_decode($_POST['post_waiting'], true);
            $waitinglist = $post_waiting['appointment'];

            $this->load->model('appointments_model');
            $this->appointments_model->waitinglist_to_db($waitinglist);
            $this->load->view('appointments/waiting_success');//return to book view
        } else {

        try {
            $post_data = json_decode($_POST['post_data'], true);
            $appointment = $post_data['appointment'];
            $customer = $post_data['customer'];

            if ($this->customers_model->exists($customer)) 
                    $customer['id'] = $this->customers_model->find_record_id($customer);

            $customer_id = $this->customers_model->add($customer);
            $appointment['id_users_customer'] = $customer_id; 

            $appointment['id'] = $this->appointments_model->add($appointment);
            $appointment['hash'] = $this->appointments_model->get_value('hash', $appointment['id']);

            $provider = $this->providers_model->get_row($appointment['id_users_provider']);
            $service = $this->services_model->get_row($appointment['id_services']);

            $company_settings = array( 
                'company_name'  => $this->settings_model->get_setting('company_name'),
                'company_link'  => $this->settings_model->get_setting('company_link'),
                'company_email' => $this->settings_model->get_setting('company_email')
            );

It returns Customers_Model->exists(NULL) and says that customers email was not provided. This is all wrong and does not happen outside the frame. It appears that I cannot postback when the application is run within the wordpress page and shortcode. So, I believe I need to do an Ajax post instead. What would that look like in this case? Would it require major surgery on the code?

Related posts

1 comment

Comments are closed.