Domande e Risposte in una Pagina PHP

Continuiamo il nostro progetto sulla creazione di un questionario sviluppato su pagine dinamiche PHP, ci sono molti modi per compiere le stesse operazioni, io ho scelto una pagina in cui vengono fatte tutte le domande.

Nella seconda parte del progetto sul questionario in PHP creiamo un’unica pagina dinamica. Lo script presenta le domande in sequenza, somma i punti ottenuti e registra il risultato finale nella tabella Classifica.

Gestione dello stato tra le domande

Lo script usa $nDomanda per seguire l’avanzamento del candidato. Usiamo 0 all’inizio. Impostiamo 10000 come sentinella per salvare il voto finale:

<?php
   $domande = mysql_connect("mio_host", "mio_utente", "mia_password")
      or trigger_error(mysql_error(), E_USER_ERROR);
   mysql_select_db("mio_database", $domande);

   $argomento = $_REQUEST['argomento'] ?? '';
   if (strlen($argomento) <= 0) {
      mysql_close($domande);
      header("location:../");
      exit();
   }

   $nDomanda = $_REQUEST['numdomanda'] ?? '';
   if (strlen($nDomanda) <= 0) $nDomanda = 0;

   $bFinish = ($nDomanda >= 10000);
   $bError = false;

   if (!$bFinish) {
      $domande_set = mysql_query("SELECT * FROM Domande WHERE Argomento = " . $argomento . " ORDER BY Domanda", $domande)
         or die(mysql_error());
      $row_domande = mysql_fetch_assoc($domande_set);
      $num_domande = mysql_num_rows($domande_set);

      if ($num_domande <= 0) {
         mysql_free_result($domande_set);
         mysql_close($domande);
         header("location:../");
         exit();
      }

      $bDomanda = true;
      if ($nDomanda <= $num_domande) {
         for ($i = 1; $i < $nDomanda; $i++)
            $row_domande = mysql_fetch_assoc($domande_set);
      } else {
         $bDomanda = false;
      }

      $nDomanda++;
      $punteggio = (int)($_REQUEST['punteggio'] ?? 0);
      $vRisposta = (int)($_REQUEST['vRisposta'] ?? 0);
      $punteggio += $vRisposta;
   } else {
      $punteggio = (int)($_REQUEST['punteggio'] ?? 0);
      $strNome = $_REQUEST['nome'] ?? '';
      if (strlen($strNome) > 0)
         mysql_query("INSERT INTO Classifica (Nome, Punteggio, Argomento) VALUES('" . $strNome . "', " . $punteggio . ", " . $argomento . ")", $domande) or die(mysql_error());
      else
         $bError = true;
   }
?>

Rendering delle schermate e passaggio parametri

La tabella HTML cambia contenuto in base a $nDomanda e $bFinish. Mostra benvenuto, domanda con tre radio button, form nickname oppure link alla classifica:

<?php
   if (!$bFinish) {
      if ($nDomanda == 1) {
         echo '<p align="center">Benvenuto nel questionario, rispondi alle domande e vedi il tuo punteggio</p>';
         echo '<form action="#" method="POST">';
         echo '<input type="submit" value="Inizia Prova">';
         echo '<input type="hidden" name="argomento" value="' . $argomento . '">';
         echo '<input type="hidden" name="punteggio" value="' . $punteggio . '">';
         echo '<input type="hidden" name="numdomanda" value="' . $nDomanda . '">';
         echo '</form>';
      } else if ($bDomanda) {
         echo '<p><b>' . $row_domande['Domanda'] . '</b></p>';
         echo '<form action="#" method="POST">';
         echo '<div><input type="radio" name="vRisposta" value="' . $row_domande['Valore1'] . '">' . $row_domande['Risposta1'] . '</div>';
         echo '<div><input type="radio" name="vRisposta" value="' . $row_domande['Valore2'] . '">' . $row_domande['Risposta2'] . '</div>';
         echo '<div><input type="radio" name="vRisposta" value="' . $row_domande['Valore3'] . '">' . $row_domande['Risposta3'] . '</div>';
         echo '<input type="submit" value="Avanti >>">';
         echo '<input type="hidden" name="argomento" value="' . $argomento . '">';
         echo '<input type="hidden" name="punteggio" value="' . $punteggio . '">';
         echo '<input type="hidden" name="numdomanda" value="' . $nDomanda . '">';
         echo '</form>';
      } else {
         echo '<p align="center">Il tuo punteggio è: <b>' . $punteggio . '</b></p>';
         echo '<form action="#" method="POST">';
         echo 'Inserisci il tuo nome: <input type="text" name="nome" maxlength="50">';
         echo '<input type="submit" value="Memorizza Punteggio">';
         echo '<input type="hidden" name="argomento" value="' . $argomento . '">';
         echo '<input type="hidden" name="punteggio" value="' . $punteggio . '">';
         echo '<input type="hidden" name="numdomanda" value="10000">';
         echo '</form>';
      }
   } else {
      if ($bError)
         echo '<p align="center">Errore nella memorizzazione del punteggio!</p>';
      else
         echo '<p align="center">Punteggio memorizzato! Vedi la <a href="classifica.php?argomento=' . $argomento . '">classifica</a>.</p>';
   }
?>

Best practice moderne: sessioni e PDO

In un’app reale non salvare il punteggio nei campi hidden. L’utente potrebbe modificarli dal browser. Salva invece stato e punti in $_SESSION sul server. Usa sempre PDO e htmlspecialchars() contro SQL Injection e XSS.

<< Lezione Precedente – Lezione Successiva >>

Vuoi continuare il percorso? Torna alla guida generale del corso su PHP: Corso di PHP

Vuoi approfondire questi argomenti? Esplora tutte le guide nella categoria PHP.

Giampaolo Rossi
Giampaolo Rossi

Sviluppatore di software gestionale da oltre 30 anni.

Articoli: 327

RGPSoft© 2026 - Contenuti distribuiti con licenza CC BY 4.0