通过json到Ajax的post将post转换回PHP


Convert post-back to PHP via json to Ajax post

我正试图通过插件将Codeigniter应用程序集成到Wordpress中。当客户确认新预约的数据时,代码点火器程序在这一点上难以控制。

查看

            <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');
    });

控制器:

        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')
            );

它返回Customers_Model->exists(NULL),并表示未提供客户电子邮件。这一切都是错误的,不会发生在框架之外。当应用程序在wordpress页面和shortcode中运行时,我似乎无法回发。所以,我认为我需要做一个Ajax的帖子。在这种情况下会是什么样子?这需要对代码进行大手术吗?

当我处理这个问题时,我一点一点地缩小了我的问题范围,并在这里发布了解决方案:AJAX提交和500服务器错误