Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php “从中插入Ajax”对话框_Php_Mysql_Ajax - Fatal编程技术网

Php “从中插入Ajax”对话框

Php “从中插入Ajax”对话框,php,mysql,ajax,Php,Mysql,Ajax,我正在尝试使用ajax从对话框中的表单向我的修复历史记录表中插入一行 以下是使用以下表单打开对话框的函数: <script> $(function() { $( "#repair-dialog" ).dialog({ autoOpen: false, height: 300, width: 450, modal: true, buttons

我正在尝试使用ajax从对话框中的表单向我的修复历史记录表中插入一行

以下是使用以下表单打开对话框的函数:

<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
        });            
    }       
下面是对话框:

<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> 

修复id:
维修日期:
评论:

任何回复都非常感谢!谢谢!:)

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

替换

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


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

$.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);    
        }

问题是什么?一些错误?我不确定这是否相关,但如果问题是将数据传递到服务器,那么有时我的问题可以通过在ajax调用中添加
contentType
选项来解决。我认为可能是这样,但仍然不起作用,感谢您,这实际上是代码的问题之一。另外,没有将http://放在绝对url前面意味着没有调用控制器!
$.ajax({
        type: "POST",
        url: "http://localhost/cibs/index.php/api/record_save/"+save_type,
        data:  mydata,
        dataType: "json",
        cache : false
    });   
        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);    
        }