如何使用复选框使用ajax更新数据库

时间:2018-02-04 17:20:52

标签: javascript php jquery ajax checkbox

我想这样做,所以当用户点击复选框时,它会将数据库中设置的tinyint值从0设置为1,反之亦然。我的ajax.js

function emailNextWithAddress(chk, address) {
    var nextEmail, inside_where;
    if (chk.checked === true) {
        $.ajax({
            url: "marca_enviado.php",
            data: address,
            type: "get",
            success: function () {
                nextEmail = document.createElement('input');
                nextEmail.id = address;
                nextEmail.value = address;
                nextEmail.type = 'text';
                nextEmail.name = 'email[]';
                nextEmail.className = 'insemail';
                nextEmail.style.display = 'inline-block';
                inside_where = document.getElementById('addEmail');
                inside_where.appendChild(nextEmail);
            },
            error: function () {
                console.log("Erro!");
            }
        });
    } else {
        $.ajax({
            url: "desmarca_enviado.php",
            data: address,
            type: "get",
            success: function () {
                inside_where = document.getElementById(address);
                inside_where.parentNode.removeChild(inside_where);
            },
            error: function () {
                console.log("Erro!");
            }
        });
    }
    return false;
}

我正在尝试更新的部分是(dbh.php是与数据库的连接):

<?php
    include_once 'dbh.php';

    $id = $_GET['address'];

    $updateEnviado = "UPDATE escolas SET Enviado = '$id'";

    $result = mysqli_query($conn , $updateEnviado);

 ?>

在我的index.php中,输出的代码部分是:

    

    $execItems = $conn->query("SELECT Nome,EmailGeral,Enviado FROM escolas");

    while($infoItems = $execItems->fetch_array()){
        echo    "
                <tr style=\"border: 1px solid black;\">
                    <td>".$infoItems['Nome']."</td>
                    <td>".$infoItems['EmailGeral']."</td>
                    <td><input type=\"checkbox\" ".($infoItems['Enviado']?' checked':' ')." onclick=\"emailNextWithAddress(this, '".$infoItems['EmailGeral']."');\"/></td>
                </tr>
            ";

    }
?>

请注意,我只对更新复选框感兴趣。

0 个答案:

没有答案