jeudi 1 février 2018

How to change database Boolean using php

Basically I want to send the same message to several recipients and I want it to register with a checkbox if the email was sent to that user or unchecked if it wasn't(The user has to check the checkbox so it registers) and I can't find a way so when a user checks a checkbox it gets updated in the database. I've tried with Ajax but I've no idea how to properly use it. I need some serious help. My index.php

<?php
    include_once 'dbh.php';  
    include_once 'contact.php'; 
?>

<!doctype html>
<html lang="en">

<head>
    <meta charset="utf-8">

    <link rel="stylesheet" href="/css/reset.css">   
    <link rel="stylesheet" href="/css/style.css">

</script>

</head>

<body>

    <br>
    <div class="tabela">
    <table class="scroll">
    <thead>

        <tr>
            <th>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp
            &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp
            &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp Nome da Escola &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp
            &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp
            &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp</th>
           <th>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspEmail Geral da Escola&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp
           &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp</th>
           <th>&nbsp&nbsp Enviado</th>
        </tr>

    </thead>

    <tbody>
        <?php

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

            while($infoItems = $execItems->fetch_array()){
                echo    "
                        <tr>
                            <td>".$infoItems['Nome']."</td>
                            <td>".$infoItems['EmailGeral']."</td>
                            <td><input type=\"checkbox\"".($infoItems['Enviado']?' checked':'checked')."\" /></td>
                        </tr>
                    ";

            }
        ?>
    </tbody>
    </table>
    <div class="mail">
    <form action="" method="post">
    <button class="butao" type="button" onclick="emailNext();">Adicionar Email</button>
    <div id="addEmail"></div>

    <script>

    function emailNext() {
    var nextEmail, inside_where;
    nextEmail = document.createElement('input');
    nextEmail.type = 'text';
    nextEmail.name = 'email[]';
    nextEmail.className = 'insemail';
    nextEmail.style.display = 'inline-block';
    nextEmail.placeholder = 'Insira o Email';
    inside_where = document.getElementById('addEmail');
    inside_where.appendChild(nextEmail);
    return false;
    }

    </script>

    Assunto:<br><textarea rows="1" name="subject" cols="30"></textarea><br>
    Mensagem:<br><textarea rows="5" name="message" cols="30"></textarea><br>
    <input class="butao" type="submit" name="submit" value="Submit">
        </form>
    </div>
    </div>
</body>

</html>

Here's the connection to the database that I've created:

<?php

$dbServername = "localhost";
$dbUsername = "root";
$dbPassword = "";
$dbName = "escoladb";

$conn = mysqli_connect($dbServername, $dbUsername, $dbPassword, $dbName);

if (mysqli_connect_errno())
{
echo "can't connect to MySQL: " . mysqli_connect_error();
}
?>

And finally the contact so I can send emails to the schools that I want:

<?php
if(isset($_POST['submit'])){
    $to = implode(',', $_POST['email']);;
    $from = "g.afonso.escola@gmail.com";
    $subject = $_POST['subject'];
    $message = $_POST['message'];

    $headers = "From:" . $from;
    mail($to,$subject,$message,$headers);

    echo "<div style=\"font-size:25px;background-color:white;text-align:center;\"> Email Enviado </div>";
    }
?>




Aucun commentaire:

Enregistrer un commentaire