• User Newbie

    Throw new exception

    salve a tutti, è da poco che utilizzo il php e mi sto esercitando.
    Ho fatto un form di inserimento dati che faccia anche una verifica dei dati inseriti aiutandomi col libbro di php.
    Mi da due errori qulcuno mi può aiutare?
    Vi posto il codice
    </head>
    <body bgcolor="#66CC66">
    <?
    if (isset($_POST[dati]))
    {
    //variabili
    $nome = $_POST[nome];
    $cognome = $_POST[cognome];
    $compleanno = $_POST[compleanno];
    $eta = $_POST[eta];
    $telefono = $_POST[telefono];
    $indirizzo = $_POST[indirizzo];
    $citta = $_POST[citta];
    $stato = $_POST[stato];
    $cap = $_POST[cap];

    $campi = array("nome" => "string", "cognome" => "string", "compleanno" => "date", "eta" => "integer", "telefono" => "string", "indirizzo" => "string", "citta" => "string", "cap" => "string");

    //controlli sui valori inseriti in ogni campo del form

    function convalida($validate)
    {
    foreach ($validate as $key => $valore)
    {
    $valore_campo = $key;
    global $valore_campo;
    // echo "Il valore del campo è" . $$valore-campo . "<br>";
    switch ($valore)
    {
    case "string";
    if((strlen($valore_campo) <1) or (strlen($valore_campo) > 15))
    {
    throw new Exception("Inserisci un valore compreso tra 1 e 15 caratteri nel campo <b> $key </b>");
    }
    break;
    case "date";
    if (!ereg ("^ [0-9]{4}-([1-9]|(0[1-9])|(1 [0-2]))-([ 1-9]|(0 [1-9])|([1-2] [0-9])|3[0-1])$", $valore_campo))
    {
    throw new Exception("Inserisci un formato di data valido come Anno-Mese-Giorno nel campo <b> $key </b>");
    }
    break;
    case "integer";
    if (!is_numeric($valore_campo))
    {
    throw new Exception ("Inserisci un valore numerico nel campo <b> $key </b>");
    }
    break;
    default;
    break;
    }
    }

    }
    //questa funzione cattura l'eccezione e produrre un messaggio di errore
    try {
    convalida($valore)
    }
    catch (Exception $e)
    {
    echo $e -> getMessage();
    echo "<br>";
    }

    }
    // se non trova errori va ava
    if(!is_object($e) and isset($dati))
    {
    echo "Grazie per la tua registrazione";
    }
    else
    {
    //restituisce il modulo compilato e chiede all'utente di provare di nuovo
    ?>
    <div align="center">
    <form method="post" action="VerificaDati.php">
    <input type="hidden" name="dati" value="true">

    <!creo una tabella>


  • ModSenior

    Ciao ferdinando26,

    Se non ci dici quali sono questi 2 errori, mi pare difficile aiutarti. 🙂


  • User Newbie

    il try catch e il throw new exception
    scusate


  • ModSenior

    Magari incolla l'errore, da errore è troppo generico.
    Secondo me facevi comunque prima ad usare dei semplici IF...