// Percorso del file JSON $file_json = "studenti.json"; // Controlla se il metodo della rachiesta รจ POST if ($_SERVER["REQUEST_METHOD"] == "POST") { // Prende i dati dal form $nuovo_studente = array( "nome" => $_POST["nome"], "cognome" => $_POST["cognome"], "eta" => intval($_POST["eta"]), "voto_medio" => floatval($_POST["voto"]) ); // Se il file JSON esiste, lo legge e aggiunge il nuovo studente if(file_exists($file_json)) { $json_data = file_get_contents($file_json); $studenti = json_decode(json_data, true); } else { $studenti = array(); } //Aggiunge il nuovo studente all'array $studenti[] = $nuovo_studente; // Converte l'array aggiornato in JSON $json_data = json_encode($studenti, JSON_PRETTY_PRINT); // Salva il JSON nel file file_put_contents($file_json, $json_data); echo "Studente aggiunto con successo!"; }else{ echo "Errore: richiesta non valida!"; } ?>