如何使用 jQuery AJAX 将数组发送到 Codeigniter

我有一系列学生的分数:


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

}

现在我的问题是:

  • 还有另一种方法可以在服务器端计算数组长度吗?

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

    如果没有

  • 是否有其他方法来创建并将它们发送到服务器?

谢谢。



一只甜甜圈
浏览 127回答 1
1回答

牧羊人nacy

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">&nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; <td><span>George</span></td>&nbsp; &nbsp; &nbsp; &nbsp; <td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input name="mark[]" type="text" id="1105"/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input name="mark_id[]" type="hidden" value="1105"/>&nbsp; &nbsp; &nbsp; &nbsp; </td>&nbsp; &nbsp; </tr>&nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; <td><span>Danny</span></td>&nbsp; &nbsp; &nbsp; &nbsp; <td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input name="mark[]" type="text" id="1351"/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input name="mark_id[]" type="hidden" value="1351"/>&nbsp; &nbsp; &nbsp; &nbsp; </td>&nbsp; &nbsp; </tr>&nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; <td><span>Linda</span></td>&nbsp; &nbsp; &nbsp; &nbsp; <td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input name="mark[]" type="text" id="3486"/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input name="mark_id[]" type="hidden" value="3486"/>&nbsp; &nbsp; &nbsp; &nbsp; </td>&nbsp; &nbsp; </tr></table><button id="save_marks">SAVE</button></form>和 JavaScript 部分$(document).on('submit', '#form_data', function (event) {&nbsp; &nbsp; event.preventDefault();&nbsp; &nbsp; var data = new FormData(this);&nbsp; &nbsp; $.ajax({&nbsp; &nbsp; &nbsp; &nbsp; //fill url with your controller name&nbsp; &nbsp; &nbsp; &nbsp; url:"<?php echo base_url(); ?>controllername/save_marks",&nbsp; &nbsp; &nbsp; &nbsp; method:"POST",&nbsp; &nbsp; &nbsp; &nbsp; data: data,&nbsp; &nbsp; &nbsp; &nbsp; async: false,&nbsp; &nbsp; &nbsp; &nbsp; processData: false,&nbsp; &nbsp; &nbsp; &nbsp; contentType: false,&nbsp; &nbsp; &nbsp; &nbsp; cache:false,&nbsp; &nbsp; &nbsp; &nbsp; dataType:"json",&nbsp; &nbsp; &nbsp; &nbsp; success:function(returndata)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //do something with your returned data&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; error: function (xhr, ajaxOptions, thrownError) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(xhr.status);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(thrownError);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });});在你的控制器中:public function save_marks() {&nbsp; &nbsp; $teacher= $this->input->post('teacher',true);&nbsp; &nbsp; $course= $this->input->post('course',true);&nbsp; &nbsp; //mark & mark_id is an array&nbsp; &nbsp; $mark= $this->input->post('mark',true);&nbsp; &nbsp; $mark_id= $this->input->post('mark_id',true);&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; $arr = array();&nbsp; &nbsp; foreach($mark_id as $key => $row)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $arr[] = array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'TEACHERS' => $teacher,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'COURSES' => $course,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'MARKS' => $mark[$key],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'ID' => $row&nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; $this->db->insert_batch('marks_table', $arr);&nbsp; &nbsp; //die($this->db->affected_rows() . ' marks were saved.');&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; echo json_encode($arr);}通过发送 formdata,您不需要手动构造数组,但是由于您需要将其发送mark_id到控制器,因此您需要再添加 1 个字段来发送mark_id
打开App,查看更多内容
随时随地看视频慕课网APP