我有一个从CGI应用程序填充的html格式的表行数据。我希望每行旁边都有一个复选框,这样我就可以删除多行,就像在gmail中一样。
我弄清楚了基本的文本表单,并能够将其发送到CGI程序以删除行,但我不希望每次必须键入行名才能删除单个文件。
当您可以通过复选框选择多个删除时,表单的两端(html浏览器和C-CGI应用程序)的代码是什么样子的?有没有什么例子?(我仅限于JS和HTML,但我认为JS是用于验证的,现在不需要它。在CGI应用程序端进行C代码编写。)
谢谢。
发布于 2009-01-09 18:03:19
好的,你可以通过几种方式来实现:
1)使所有元素具有相同的形式。将每个复选框命名为相同的名称,但为每个复选框指定一个值,以区分其所代表的记录/id/文件。当浏览器提交表单时,CGI应用程序应该能够看到HTTP参数作为POST或GET提交的一部分。许多CGI应用程序(如PHP )将同名参数组合到一个数组中。你也可以用C自己遍历参数列表。
// Client side html
<table>
<form>
<tr><td><input type="checkbox" name="id" value="1"/></td><td>Row 1</td></tr>
<tr><td><input type="checkbox" name="id" value="2"/></td><td>Row 2</td></tr>
<tr><td><input type="checkbox" name="id" value="3"/></td><td>Row 3</td></tr>
<tr><td><input type="checkbox" name="id" value="4"/></td><td>Row 4</td></tr>
</form>
</table>
// Server side CGI, using pseudo-code
String[] ids = request.getArrayOfParametersNamed("id");
if(!empty(ids)) {
for(id in ids) {
DatabaseControllerModelThingWhatever.deleteById(id);
}
// Actually if SQL based you should use a batch statement instead of
// one-at-a-time deletes like above
}
// Ok the rows are deleted, either print out the page, or better yet,
// send a redirect so that a user-refresh does not try and re-delete
// already deleted stuff and also give the user a wierd "resubmit form" warning
// Done
2)使用AJAX,并且优选地使用某种类型的Javascript库,当用户点击删除时,执行基于ajax的提交,该提交提交请求以删除选中的记录。同时使用Javascript从HTML表中删除行。这意味着用户的页面永远不会完全刷新。
// Client side HTML is same as before, only this time there is a DELETE button with
// an onclick handler. Also, add a "class" or "id" to each "tr" so we can find it
// in the HTML table
// Pseudo-javascript because I am lazy
function onDeleteButtonClick() {
// Get our ids
var idElements = document.getElementsById("id");
// Submit an async AJAX request (e.g. use Jquery and send ids as URL params)
ajaxedDeleteSubmission(idElements);
// Delete all the rows that should not be there
for(i = 0; i < tablex.rows.length; i++) {
// Grab the value of the "id" attribute of each table row (<tr id="?">...</tr>)
id = tablex.rows[id].id;
if(id in ids) {
// Remove the row, forget how because now I just use Jquery.
tablex.deleteRow(i);
}
}
}
发布于 2009-01-09 17:54:57
看看"AJAX“风格的javascript。当您向服务器发出AJAX请求时,传递所有删除内容。服务器端应该编码为在单个请求中接受多个删除。
https://stackoverflow.com/questions/430314
复制