[英]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
上方的“删除内容”列中有一个按钮,该按钮将调用一种方法来删除该条目。 我一直想做的是在每行的末尾添加一个支票簿,并向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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.