AJAX:将一组值发布到php
AJAX: post an array of values to php
我对javascript/jquery:几乎一无所知
我有一个ajax调用,它返回格式化为html表的条目,如下所示:
Stuff | Other stuff | delete stuff
------|----------------|-------------------------
value1| from database | delete this entry button
------|----------------|-------------------------
value2| from database | delete this entry button
上面写着"delete stuff"的列有一个按钮,它将调用一个方法来删除该条目。我一直在尝试在每一行的末尾添加一个复选框,并向php发送一个数组来删除多个记录。
电话是:
$('#menu-notifications').click(function() {
$.ajax({
type: 'GET',
url: '/admin/notifications/ajax_view_notifications/' + <?= $this->session->userdata('user_id'); ?>,
dataType: 'json',
cache: false,
success: function(data) {
$("#notifications-modal .modal-body table").find("tr:gt(0)").remove();
$.each(data, function(i, obj) {
$('#notifications-modal .modal-body table tr:last').after(
'<tr><td>' + htmlDecode(obj.message) + '</td>' +
'<td>' + obj.created_by + '</td>' +
'<td>' + obj.created_dt + '</td>' +
'<td><div class="btn-group">' +
'<a href="/admin/notifications/ajax_delete_notification/' + obj.id + '" class="btn btn-mini delete-notification" id="delete-notification" data-dismiss="modal"><i class="icon-remove"></i></a>' +
// here should be a checkbox to mark for deletion
'</div></td></tr>');
});
}
});
我已经成功添加了复选框,但每次尝试使用<?=form_open();?>
或打开另一个表单都会导致页面根本无法加载(控制台中没有任何内容)。
总结一下:我试图在每行的末尾附加一个复选框,我可以标记这个复选框,并将每个标记的复选框发送到一个方法。
您需要用HTML数组创建每个复选框,它的值将是元素id,然后您需要一个操作(如"Delete All"按钮),用Ajax将数据发送到PHP(不需要表单):
$('#menu-notifications').click(function() {
// Simpler: use getJSON
$.getJSON('/admin/notifications/ajax_view_notifications/' + <?= $this->session->userdata('user_id'); ?>)
// Clearner: use promises :)
.done(function(data) {
$("#notifications-modal .modal-body table").find("tr:gt(0)").remove();
$.each(data, function(i, obj) {
// Create a row
var row = $('<tr>');
// Crete cells
var cells =
'<td>' + htmlDecode(obj.message) + '</td>' +
'<td>' + obj.created_by + '</td>' +
'<td>' + obj.created_dt + '</td>' +
// We use this identified cell to create our delete functionality
'<td class="delete-row"></td>';
// Fill the row
row.append(cells);
// Create the delete button
var deleteButton = $('<a>', {
'href': '/admin/notifications/ajax_delete_notification/' + obj.id
,'class': 'btn btn-mini delete-notification'
// We can't have duplicate ids, so no ids here
// ,id: 'delete-notification'
,'data-dismiss':'modal'
,'html': '<i class="icon-remove"></i>'
});
// Crete the checkbox
var checkbox = $('input', {
'type': 'checkbox'
// We use an HTML array
,'name': 'rowsToDelete[]'
,'value': obj.id
});
// Append the button and the checkbox to the row
// I ignore the .btn-group on purpose
row.find('.delete-row')
.append(deleteButton)
.append(checkbox);
// We add the row to the DOM
$('#notifications-modal .modal-body table tr:last').append(row);
});
});
});
// To delete in group we use a different call, let's say, a 'Delete All' button
$('#delete-all').click(function() {
// We serialize the info of all the buttons
// Find all checkbox under the .delete-row class and serialize it
data = $('.delete-row').find(':checkbox').serialize();
// Send the data to PHP
$.post('/admin/notifications/ajax_delete_notification/delete_a_bunch', data)
// PHP would receive $_POST['rowsToDelete'] = [1, 2, 4, 20]
.done(function(data) {
alert('All selected rows were deleted');
})
.fail(function() {
alert('No rows deleted :/');
});
});
测试片段
// To delete in group we use a different call, let's say, a 'Delete All' button
$('#delete-all').click(function() {
// We serialize the info of all the buttons
// Find all checkbox under the .delete-row class and serialize it
data = $('.delete-row').find(':checkbox').serialize();
// Alert the data (test only)
$("#r").text(data.replace(/deleteRow/g, '$_POST[deleteRow]').replace(/%5B%5D/g, '[]').replace(/'&/g, ";'n") + ';');
/*
// Send the data to PHP
$.post('/admin/notifications/ajax_delete_notification/delete_a_bunch', data)
// PHP would receive $_POST['rowsToDelete'] = [1, 2, 4, 20]
.done(function(data) {
alert('All selected rows were deleted');
})
.fail(function() {
alert('No rows deleted :/');
});
*/
});
table {
width: 100%;
}
th, td {
border-bottom: 1px solid #ddd;
text-align: left;
}
#r {
display: block;
border-top: 1px solid #ddd;
padding: 10px 0;
margin-top: 10px;
height: 50px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
<table>
<thead>
<tr>
<th>Dummy header</th>
<th>Dummy header</th>
<th>Dummy header</th>
<th>Check to delete</th>
</tr>
</thead>
<tbody>
<tr>
<td>Dummy Text</td>
<td>Dummy Text</td>
<td>Dummy Text</td>
<td class="delete-row">
<input type="checkbox" name="deleteRow[]" value="2">
</td>
</tr>
<tr>
<td>Dummy Text</td>
<td>Dummy Text</td>
<td>Dummy Text</td>
<td class="delete-row">
<input type="checkbox" name="deleteRow[]" value="3">
</td>
</tr>
<tr>
<td>Dummy Text</td>
<td>Dummy Text</td>
<td>Dummy Text</td>
<td class="delete-row">
<input type="checkbox" name="deleteRow[]" value="6">
</td>
</tr>
<tr>
<td>Dummy Text</td>
<td>Dummy Text</td>
<td>Dummy Text</td>
<td class="delete-row">
<input type="checkbox" name="deleteRow[]" checked value="10">
</td>
</tr>
</tbody>
</table>
<hr>
<button id="delete-all">Delete all elements</button>
<pre id="r"></pre>
相关文章:
- fluxxor向一个flux实例添加一组以上的操作
- 调用php数组中的JS函数
- 我们怎样才能将内含子J用于一组特定的元素
- 将PHP变量分配给PHP数组
- php数组转换为javascript
- 如何用javascript打印php数组
- 将PHP数组传递给js变量
- javascript变量到php数组
- 使用linq.js按当前日期订购一组momentjs日期
- 如何将JSON编码的PHP数组发送到同一页面上的数据表
- 正则表达式,从一组关键字中获取奇数
- 创建一组元素JointJS
- 如何在PHP数组中加载下一个youtube
- 如何在javascript中获取一组复选框值
- 使用Angular.js访问php数组数据
- 一组(共组..)
- 使用JS将页面中的每个字母设置为随机颜色.为什么在页面的每个打开标记之前总是有一组额外的span标记
- AJAX:将一组值发布到php
- 使用JavaScript将值传递到PHP文件,并从PHP接收一组新的值到JavaScript,然后在浏览器中显示
- 将多个PHP数组转换为一个数组,以便转换为Javascript