【问题标题】:Submitting checkboxes with jquery and ajax [closed]使用 jquery 和 ajax 提交复选框 [关闭]
【发布时间】:2013-02-13 01:07:05
【问题描述】:

所以我在 php 页面中有一个表格:

$page .='<form method="POST" action="delete.php" ID="orgForm">';
$page .= "<table> \n";
$page .= "<tr>\n";
//Decides what to display based on logged in. Only if logged in can you see all the contact's info
$page .= "<th>ID</th> \n <th>First Name</th> \n <th>Last Name</th> \n <th>Phone Number</th> \n <th>Email</th> \n";
//Loops through each contact, displaying information in a table according to login status
$sql2="SELECT cID, firstName, lastName, phoneNum, email FROM Contact WHERE oID=".$_GET['orgID'];
$result2=mysql_query($sql2, $connection) or die($sql1);
while($row2 = mysql_fetch_object($result2))
{
    $page .= "<tr>\n";
    $page .= "<td>".$row2->cID."</td>\n";
    $page .= "<td>".$row2->firstName."</td>\n";
    $page .= "<td>".$row2->lastName."</td>\n";
    $page .= "<td>".$row2->phoneNum."</td>\n";
    $page .= "<td>".$row2->email."</td>\n";
    $page .= '<td><input type="checkbox" name="checkedItem[]" value="'.$row2->cID.'"></input></td>'."\n";
    $page .="</tr>";
}
$page .= '<input name="deleteContacts" type="submit" value="Delete Selected Contacts" />'."\n";
$page .= "</form>\n";

$page .='<script src="assets/js/orgDetails.js" type="text/javascript"></script>'."\n";

我需要以某种方式在 orgDetails.js 中编写一个 jquery 脚本,当我按下删除按钮时,该脚本能够删除选中的行。更改必须在不刷新的情况下出现在屏幕上,而且我还需要能够从 sql db 中删除实际行。有人可以帮帮我吗?谢谢。

【问题讨论】:

    标签: php jquery ajax forms checkbox


    【解决方案1】:

    在action url delete.php中,提交此帖后:

    if ($_POST != array()) {
        foreach ($_POST['checkedItem'] as $id) {
            mysql_query('delete from Contact where cID='.$id);
        }
    
        echo 'Records deleted.';
    }
    

    如果你不想在删除记录时刷新页面:

    添加到 html:

    <button class="delete_button">Delete selected records</button>
    

    添加你的 js 文件:

    $('.delete_button').click(function () {
        $form = $('#orgForm');
    
        delete_ids = [];
    
        $form.find('input[name=checkedItem]').each(function () {
            $checkbox = $(this);
    
            if ($checkbox.is(':checked')) {
                delete_ids.push($checkbox.val());
            }
        );
    
        $.ajax({
            url: 'delete.php',
            type: 'post',
            data: {delete_ids: delete_ids},
            success: function (result_html) { alert(result_html); },
        });
    });
    

    在delete.php中:

    if ($_POST != array()) {
        foreach ($_POST['delete_ids'] as $id) {
            mysql_query('delete from Contact where cID='.$id);
        }
    
        echo 'Records deleted.';
    }
    

    【讨论】:

      猜你喜欢
      • 2013-02-12
      • 2017-04-01
      • 2012-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-20
      • 2020-06-22
      相关资源
      最近更新 更多