【问题标题】:How can I send an array to Codeigniter with jQuery AJAX如何使用 jQuery AJAX 将数组发送到 Codeigniter
【发布时间】:2020-10-23 00:41:01
【问题描述】:

我有一系列学生的分数:

<span>Teacher Name : </span> <input id="teacher" type="text">
<span>Course Name : </span> <input id="course" type="text">
<table id="students_list">
    <tr>
        <td><span>George</span></td>
        <td><input class="mark-field" type="text" id="1105"/></td>
    </tr>
    <tr>
        <td><span>Danny</span></td>
        <td><input class="mark-field" type="text" id="1351"/></td>
    </tr>
    <tr>
        <td><span>Linda</span></td>
        <td><input class="mark-field" type="text" id="3486"/></td>
    </tr>
    <tr>
        <td><span>Mario</span></td>
        <td><input class="mark-field" type="text" id="9032"/></td>
    </tr>
    …
</table>
<button id="save_marks">SAVE</button>

我使用这种方法用 JQUERY 创建一个数组并将其发送到服务器:

$(document).on('click', '#save_marks', function () {
    var dataArray = [];
    var i = 1;
    $('.mark-field').each(function () {
        dataArray[i] = {
            'teacher' : $('#teacher').val(),
            'course' : $('#course').val(),
            'mark' : $(this).val(),
            'id' : $(this).attr('id')
        };
        i++;
    });
    dataArray[0] = i;
    $.ajax({
        url: 'save-marks',
        data: {dataset: dataArray},
        type: 'post',
        success: function (res) {
            alert(res);
        }
    });
});

并使用这种方式将其更改为 PHP (CodeIgniter) 数组并将其保存在数据库中:

public function save_marks() {
    $arrayLength = $this->input->post('data')[0];
    for ($i = 1; $i < $arrayLength; $i++) {
        $arr[] = array(
            'TEACHERS' => $this->input->post('dataset')[$i]['teacher'],
            'COURSES' => $this->input->post('dataset')[$i]['course'],
            'MARKS' => $this->input->post('dataset')[$i]['mark'],
            'ID' => $this->input->post('dataset')[$i]['id']
        );
    }
    $this->db->insert_batch('marks_table', $arr);
    die($this->db->affected_rows() . ' marks were saved.');
}

现在我的问题:

  • 有没有其他方法可以在服务器端计算数组长度?

  • 在服务器端和客户端都构建数组是一种好方法吗?

    如果没有

  • 还有其他方法可以创建它们并将它们发送到服务器吗?

谢谢。

【问题讨论】:

  • 如果已有数组,为什么要重新创建?
  • 您可以转换为 json 并将其设置为字符串,或者尝试使用 data: {'dataset[]': dataArray},

标签: php jquery arrays ajax codeigniter


【解决方案1】:

1.是否有另一种方法可以在服务器端计算数组长度?

可以,通过sizeof($array),可以得到数组的数组长度。

2。在服务器端和客户端都构建数组是一种好方法吗?

使用name="mark-field[]",您可以发送标记列表,而无需在您的javascript中手动构建它,也可以使用sizeof($array),您可以在服务器端获取数组大小,而无需从您的javascript发送大小。

3.还有其他方法可以创建它们并将它们发送到服务器吗?

就个人而言,我会这样做:

<form id = "form_data" method="post">
<span>Teacher Name : </span> <input id="teacher" name="teacher" type="text">
<span>Course Name : </span> <input id="course" name="course" type="text">
<table id="students_list">
    <tr>
        <td><span>George</span></td>
        <td>
            <input name="mark[]" type="text" id="1105"/>
            <input name="mark_id[]" type="hidden" value="1105"/>
        </td>
    </tr>
    <tr>
        <td><span>Danny</span></td>
        <td>
            <input name="mark[]" type="text" id="1351"/>
            <input name="mark_id[]" type="hidden" value="1351"/>
        </td>
    </tr>
    <tr>
        <td><span>Linda</span></td>
        <td>
            <input name="mark[]" type="text" id="3486"/>
            <input name="mark_id[]" type="hidden" value="3486"/>
        </td>
    </tr>
</table>
<button id="save_marks">SAVE</button>
</form>

还有 javascript 部分

$(document).on('submit', '#form_data', function (event) {
    event.preventDefault();
    var data = new FormData(this);
    $.ajax({
        //fill url with your controller name
        url:"<?php echo base_url(); ?>controllername/save_marks",
        method:"POST",
        data: data,
        async: false,
        processData: false,
        contentType: false,
        cache:false,
        dataType:"json",
        success:function(returndata)
        {
            //do something with your returned data
        },
        error: function (xhr, ajaxOptions, thrownError) {
            console.log(xhr.status);
            console.log(thrownError);
        }
    });
});

在你的控制器中:

public function save_marks() {
    $teacher= $this->input->post('teacher',true);
    $course= $this->input->post('course',true);
    //mark & mark_id is an array
    $mark= $this->input->post('mark',true);
    $mark_id= $this->input->post('mark_id',true);
    
    $arr = array();
    foreach($mark_id as $key => $row)
    {
        $arr[] = array(
            'TEACHERS' => $teacher,
            'COURSES' => $course,
            'MARKS' => $mark[$key],
            'ID' => $row
        );
    }
    
    $this->db->insert_batch('marks_table', $arr);
    //die($this->db->affected_rows() . ' marks were saved.');
    
    echo json_encode($arr);
}

通过发送formdata,您不需要手动构造数组,但是由于您需要将mark_id发送到控制器,您需要再添加1个字段来发送mark_id

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-06
    • 2012-10-25
    • 1970-01-01
    • 2013-01-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多