亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用 jQuery AJAX 將數組發送到 Codeigniter

如何使用 jQuery AJAX 將數組發送到 Codeigniter

PHP
一只甜甜圈 2023-07-08 20:16:51
我有一系列學生的分數:<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.');}現在我的問題是:還有另一種方法可以在服務器端計算數組長度嗎?在服務器端和客戶端都構建數組是一個好方法嗎?如果沒有是否有其他方法來創建并將它們發送到服務器?謝謝。
查看完整描述

1 回答

?
牧羊人nacy

TA貢獻1862條經驗 獲得超7個贊

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


查看完整回答
反對 回復 2023-07-08
  • 1 回答
  • 0 關注
  • 161 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號