dimanche 4 février 2018

How to update database using ajax using checkboxes

I want to do it so when a user clicks a checkbox it sets the tinyint value that's set in the database from 0 to 1 and vice-versa. My 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;
}

And the part that I'm trying to update is(dbh.php is the connection to the database):

<?php
    include_once 'dbh.php';

    $id = $_GET['address'];

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

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

 ?>

In my index.php the part of the code that's being outputted is:

    $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>
            ";

    }
?>

Note that I'm only interested in updating the checkbox.




Aucun commentaire:

Enregistrer un commentaire