我的JavaScript的奇怪行为

Sorry for the vague title, but this behaviour is pretty hard to explain.

I have a form where I enter some information. When the user click the submit button (values "programma"), I call a javascript. I check that the info are correct and use AJAX to add a record in my local database. The strange behaviour is the following:

  • I fill up the form. The record doesn't exist in my DB. Everything works correctly and my record is inserted. Success: I write in the span tag "Esecuzione registrata correttamente!"
  • Then I try to insert the same record. Since it exists in my DB, the error gets notified correctly in my span: "Esiste un condannato con gli stessi dati"
  • At this point my form is still full of text, if the user want to correct his mistake and insert a non-existing record. What happens is that, when I change some input (and I'm sure that record doesn't exist in my DB) and click the submit button, literally nothing happens. I still see my error message "Esiste un condannato con gli stessi dati", even if I saw in my console that my AJAX worked correctly

Screenshot

programma.php (what you see in my screenshot)

<?php
    session_start();

    if(!isset($_SESSION["nickname"]) && !isset($_SESSION["password"]) ) {
        header("location: index.php");
        exit();
    }
    else
    {
        echo "<p> Sessione di ".$_SESSION["nickname"]." con password ".$_SESSION["password"]." </p>";
    }
?>

<!DOCTYPE html>
<html lang="it">
    <head>
        <meta charset="UTF-8">
        <link rel="stylesheet" href="programma.css">
        <style type="text/css">
            .container {
                width: 500px;
                clear: both;
            }
            .container text {
                width: 100%;
                clear: both;
            }
        </style>
      <title>RIPconvicts - Programma esecuzione</title>
    </head>
    <body>

        <?php require 'snippets/menu.php'; ?>
        <form>
            <fieldset class="centered">
                <legend>Programma nuova esecuzione</legend>

                <label for="nome"> Nome* </label> <br>
                <input type="text" name="nome" id="nome"> <br> <br>

                <label for="cognome"> Cognome* </label> <br> 
                <input type="text" name="cognome" id="cognome"> <br> <br>

                <label for="classe"> Classe* </label> <br>
                <input type="text" name="classe" id="classe"> <br> <br>

                <label for="materia"> Materia* </label> <br>
                <input type="text" name="materia" id="materia"> <br> <br>

                <label for="data">Data* </label> <br>
                <input type="date" name="data" id="data"> <br> <br>

                Note <br>
                <textarea name="note" id="note" rows="6" cols="50"> </textarea> <br> <br>

                <span id="avviso" style="color:red"></span> <br>

                <input type="button" onclick="programma_esecuzione()" value="Programma">

            </fieldset>
        </form>
        <?php require 'snippets/footer.php'; ?>

        <script>
            function programma_esecuzione() {
                // Verifica che tutti i campi necessari siano stati riempiti
                document.getElementById("avviso").innerHTML = "";
                var nomi = ["nome", "cognome", "classe", "materia"];
                var campi_mancanti = "";

                for(var i=0; i<nomi.length-1; i++) {
                    if(document.getElementById(nomi[i]).value == "") {
                        campi_mancanti+=" "+nomi[i];
                    }
                }

                if(!Date.parse( document.getElementById("data").value )) {
                    campi_mancanti+=" data";
                }

                // Se ci sono campi mancanti, ritorna alla pagina principale
                if(campi_mancanti!="") {
                    window.alert("Riempire i seguenti campi mancanti:"+campi_mancanti);
                    return;
                }

                // Se tutti i campi necessari ci sono, allora è possibile richiedere l'aggiunta del record
                var xhttp;
                xhttp = new XMLHttpRequest();

                var nome = document.getElementById("nome").value;
                var cognome = document.getElementById("cognome").value;
                var classe = document.getElementById("classe").value;
                var materia = document.getElementById("materia").value;
                var data = document.getElementById("data").value;
                var note = document.getElementById("note").value;

                xhttp.onreadystatechange = function() {
                    if (this.readyState == 4 && this.status == 200) {
                        var risposta = this.responseText;

                        if(risposta.indexOf("Esecuzione registrata correttamente!") > -1) {

                            // Svuota tutti i campi
                            document.getElementById("nome").value = "";
                            document.getElementById("cognome").value = "";
                            document.getElementById("classe").value = "";
                            document.getElementById("materia").value = "";
                            document.getElementById("data").value = "";
                            document.getElementById("note").value = "";

                        }

                        document.getElementById("avviso").innerHTML = risposta;
                    }
                };

                xhttp.open("POST", "./snippets/programma_esecuzione.php", false);
                xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
                xhttp.send("nome="+nome+"&cognome="+cognome+"&classe="+classe+"&materia="+materia+"&data="+data+"&note="+note);
            }
        </script>
    </body>
</html>

programma_esecuzione.php (where I execute my query)

<?php
    //Include
    require 'connetti_a_DB.php';

    // Variabili per il login
    $nome = $_POST["nome"];
    $cognome = $_POST["cognome"];
    $classe = $_POST["classe"];
    $materia = $_POST["materia"];
    $data = $_POST["data"];
    $note = $_POST["note"];

    /*echo $nome."<br>";
    echo $cognome."<br>";
    echo $classe."<br>";
    echo $materia."<br>";
    echo $data."<br>";
    echo $note."<br>";*/

    // Inizia una sessione
    session_start();
    $username = $_SESSION["nickname"];
    $password = $_SESSION["password"];

    // Stabilire e chiudere connessione
    $conn = connetti_a_DB($username, $password);

        // Verifica se c'è un record "clone"
        $nome_completo = $nome." ".$cognome;
        $sql = " SELECT *
                FROM `lista`
                WHERE `NOME_COMPLETO`='$nome_completo' OR
                `CLASSE`='$classe' OR
                `MATERIA` ='$materia' OR
                `DATA` ='$data'";

        $result = $conn->query($sql);

        if($result->num_rows > 0) {
            echo 'Esiste un condannato con gli stessi dati';
        }
        else
        {
            $sql = "INSERT INTO `ripconvicts`.`lista` (`ID`, `NOME_COMPLETO`, `CLASSE`, `MATERIA`, `DATA`, `NOTE`)
                    VALUES (NULL, '$nome_completo', '$classe', '$materia', '$data', '$note')";
            $result = $conn->query($sql);
            echo "Esecuzione registrata correttamente!";
        }

    $conn->close();

    // Salta alla pagina iniziale
    //header("location: ../programma.php");
    //exit();
?>