使用Ajax param的Codeigniter SQL表更新无法正常工作

时间:2017-09-18 21:44:30

标签: php mysql ajax codeigniter

我希望有人可以帮助我,因为我已经尝试过并且试图找到错误而没有运气,我正在尝试执行更新SQL语句,该语句从POST AJAX请求获取值并且一切似乎都有效好的,但表的值不会更新。

App流是这样的(它实际上是有效的,只是没有数据更新):

在我的视图中,我有一个按钮,它引爆一个模态窗口供用户在4个文本字段中输入值,然后用户可以单击一个按钮,它通过POST AjAX调用,然后是控制器和方法负责更新,然后用户被发送回他/她最初点击按钮的同一页面。

作为附加信息,路径是正确的,流程正常,值正在通过AJAX调用正确发送到控制器但我从那时起就迷失了......:S

不确定我做错了什么,感谢任何帮助

//我的视图 - HTML按钮//

<a href="javascript:" onclick="service_report_additional_data(<?php echo $jobDetails[0]['job_id'] ?>)" class="dark_grey"><i class="fa fa-file fa-2x" aria-hidden="true" ></i></a>

//我的视图 - HTML模式窗口代码//

<div id="confirmation_box_service_report_additional_data" title="" style="display:none;">
    <h1 align="center" style="color: #333333"><?php echo $this->lang->line('service_report_title') ?></h1><br />
    <h2 align="center" style="color: #DF9E0A"><strong><?php echo $this->lang->line('project_service_comments') ?></strong></h2><br />
    <form id="form_service_report_additional_data" name="form_service_report_additional_data" method="post" action="">
        <table width="100%">
          <tr>
            <td width="45%" class="red_cell_background">
                <div class="form-group">
                  <label for="client_images_comments"><?php echo $this->lang->line('client_images_comments') ?></label>
                  <textarea class="form-control" rows="5" id="client_images_comments"></textarea>
                </div>            
            </td>
            <td width="5%" class="purple_cell_background">&nbsp;</td>
            <td width="45%" class="purple_cell_background"><div class="form-group">
              <label for="specialist_images_comments"><?php echo $this->lang->line('specialist_images_comments') ?></label>
              <textarea name="specialist_images_comments" rows="5" class="form-control" id="specialist_images_comments"></textarea>
            </div></td>
          </tr>
          <tr>
            <td class="red_cell_background"><div class="form-group">
              <label for="spare_parts_comments"><?php echo $this->lang->line('spare_parts_comments') ?></label>
              <textarea name="spare_parts_comments" rows="5" class="form-control" id="spare_parts_comments"></textarea>
            </div></td>
            <td class="purple_cell_background">&nbsp;</td>
            <td class="purple_cell_background"><div class="form-group">
              <label for="final_comments"><?php echo $this->lang->line('final_comments') ?></label>
              <textarea name="final_comments" rows="5" class="form-control" id="final_comments"></textarea>
            </div></td>
          </tr>
        </table>    
    </form>
</div>

//我的Javascript模态窗口代码//

function service_report_additional_data(jobId) {
    $("#spare_parts_comments").val("<?php echo $jobDetails[0]['spare_parts_description']; ?>");

    $("#confirmation_box_service_report_additional_data").dialog({
        autoOpen: false, 
        resizable: false,
        width: 900,
        heightStyle: "content",
        modal: true,
        closeOnEscape: false,
        open: function(event, ui) {
            $(".ui-dialog-titlebar-close", ui.dialog | ui).hide();
        },      

         buttons: [
                {
                    text: "<?php echo $this->lang->line('save_and_close'); ?>",
                    class: "btn btn-warning",
                    click: function() {

                        var clientImagesComments = $('#client_images_comments').val();
                        var technicianImagesComments = $('#specialist_images_comments').val();
                        var sparePartsDescription = $('#spare_parts_comments').val();
                        var finalThoughts = $('#final_comments').val();

                                    //alert(finalThoughts)
                        servicereportadditionaldatasaving(jobId, clientImagesComments, technicianImagesComments, sparePartsDescription, finalThoughts);

                    },
                },
                {
                    text: "<?php echo $this->lang->line('Cancel'); ?>",
                    class: "btn btn-default",
                    click: function() {
                        $(this).dialog("close");
                    }
                }
            ],
            close: function() {
                $(this).dialog("close");
            }     

        });

            $("#confirmation_box_service_report_additional_data").dialog('open');       
}

//我的AjAX调用//

function servicereportadditionaldatasaving(jobId, clientImagesComments, technicianImagesComments, sparePartsDescription, finalThoughts) {
                //alert(clientImagesComments)
   $.ajax({
            type: "POST",
            url: "<?php echo base_url() . DISPATCHERADMIN . '/jobs/servicereportadditionaldatasaving/' ?>",
            data: {
                'jobId':                    jobId,
                'clientImagesComments':     clientImagesComments,
                'technicianImagesComments': technicianImagesComments,
                'sparePartsDescription':    sparePartsDescription,
                'finalThoughts':            finalThoughts
            },
            success: function (data){
                //alert(data);
                location.href = '<?php echo base_url() . DISPATCHERADMIN . '/myjob/details/' ?>' + jobId;
            },
            error: function (jqXHR, textStatus, errorThrown){
                console.log('error:: ' + errorThrown);
            }
        });
}  

//我的控制器//

<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Jobs extends CI_Controller {

    public function __construct() {
        // Construct the parent class
        parent::__construct();
        $this->lang->load('message', 'english'); // language file
        $this->load->model('dispatcher/jobs_model');
    }

    public function servicereportadditionaldatasaving() {       

        $postData = $this->input->post();
        $data = array(
                'table_name'                    => 'jobs',
                'job_id'                        =>  $postData['jobId'],
                'client_images_comments'        =>  $postData['clientImagesComments'],
                'technician_images_comments'    =>  $postData['technicianImagesComments'],
                'spare_parts_description'       =>  $postData['sparePartsDescription'],                                     
                'final_thoughts'                =>  $postData['finalThoughts']                                                      
        );
        $this->jobs_model->update_job_service_report_additional_data($data);
    }

}
?>

//我的模型//

<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Jobs_model extends CI_Model {

    public function __construct() {
        parent::__construct();
    }

    public function update_job_service_report_additional_data($data) {      

        extract($data);
        $this->db->where("job_id", $job_id);
        $success = $this->db->update($table_name, array('client_images_comments'        => $client_images_comments,
                                                        'technician_images_comments'    => $technician_images_comments,
                                                        'spare_parts_description'       => $spare_parts_description,
                                                        'final_thoughts'                => $final_thoughts
                                            ));
        if($success) {
            return $success;
        } else {
            return false;
        }
    }
}

?>

1 个答案:

答案 0 :(得分:0)

我自己找到了答案,经过多次尝试查看代码后,我发现问题出在以下几行的Ajax调用中:

url: "<?php echo base_url() . DISPATCHERADMIN . '/jobs/servicereportadditionaldatasaving/' ?>",

它希望将ID作为原始URL的一部分传递,如下所示:

url: "<?php echo base_url() . DISPATCHERADMIN . '/jobs/servicereportadditionaldatasaving/' ?>" + jobId,

说实话,我需要更详细地检查我的代码,但这就是问题,一切都按预期工作。