1 回答

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
- 1 回答
- 0 關注
- 161 瀏覽
添加回答
舉報