繁体   English   中英

从对话框插入Ajax

[英]Ajax insert from dialog box

我正在尝试使用ajax从对话框内的表单向我的repair_history表中插入一行。

这是打开具有以下形式的对话框的函数:

<script>
    $(function() {

        $( "#repair-dialog" ).dialog({
            autoOpen: false,
            height: 300,
            width: 450,
            modal: true,
            buttons: {
                "Save": function() {
                    insert(
                        $( "#repair-dialog-date" ).val(),                            
                        $( "#repair-dialog-id" ).val(),
                        $( "#repair-dialog-comment" ).val(),
                        $( "#repair-dialog-save_type" ).val()
                    );
                    $( this ).dialog( "close" );
                    setTimeout(function(){location.reload(true);},500);
                },
                "Cancel": function() {
                    $( this ).dialog( "close" );
                }
            },
            close: function() {
            }
        });

        $( "#record_repair" ).click(function() { $( "#repair-dialog" ).dialog( "open" ); });
    });     

    function insert(date,id,comment,save_type) {

        mydata = {
                "date"      : date ,                    
                "id"      : id    ,
                "comment"   : comment , 
                "camera_id" : "<?php= $products['camera_id']?>" };

        $.ajax({
            type: "POST",
            url: "localhost/cibs/index.php/api/record_save/"+save_type,
            data:  {data:JSON.stringify(mydata)},
            dataType: "json",
            cache : false
        });            
    }       

这是api.php控制器中尝试插入的函数:

function record_save()
    {            
        $this->load->database();        
        $mydata =   $this->input->post('data');         

        $date       = $mydata['date'];            
        $comment    = $mydata['comment']; 
        $id       = $mydata['id']; 
        $camera_id  = $mydata['camera_id'];     

    $table = "repair_history";            
        $data = array("camera_id" => $camera_id, "repair_id" => $id, "date" => $date, "comment" => $comment);       

    $this->db->insert($table,$data);    
    }

这是对话框:

<div id="repair-dialog" title="Add New Repair" style="font-size: 15px;">
<form id="repair">
        <input style="height:0px; top:-1000px; position:absolute" type="text" value="">

        Repair id: <input type="text" id="repair-dialog-id" /><br>
        Repair date: <input type="text" id="repair-dialog-date" /><br>
        Comment: <input type="text" id="repair-dialog-comment" /><br>
        <input type="hidden" value="repair" id="repair-dialog-save_type">
</form>
</div> 

任何答复都表示赞赏! 谢谢! :)

您不需要将对象字符串化:

$.ajax({
        type: "POST",
        url: "http://localhost/cibs/index.php/api/record_save/"+save_type,
        data:  mydata,
        dataType: "json",
        cache : false
    });   

并在php中:

        function record_save()
        {            
            $this->load->database();   

            $table = "repair_history";            
            $data = array(
                "camera_id" => $this->input->post('camera_id'), 
                "repair_id" => $this->input->post('id'), 
                "date" => $this->input->post('date'), 
                "comment" => $this->input->post('comment')
            );       

            $this->db->insert($table,$data);    
        }

我不确定您的函数$this->input->post('data')工作方式,但是您将数据作为json字符串发布,因此应确保首先对数据进行json_decode。

更换

$mydata =   $this->input->post('data'); 

通过

$mydata =   json_decode($this->input->post('data'), true); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM