[PHP] form messaggio di conferma

  • Creatore Discussione Creatore Discussione anto4392
  • Data di inizio Data di inizio

anto4392

Nuovo Utente
22 Mag 2017
25
1
3
33
Salve,
ho creato un form per la registrazione.
Vorrei che una volta fatti tutti i controlli, se corretti, comparisse un messaggio di conferma della registrazione.

Ecco il codice

Codice:
<?php
// define variables and set to empty values
$nameErr = $emailErr = $bdayErr = $cityErr = "";
$name = $email = $bday = $city =  "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
  if (empty($_POST["name"])) {
    $nameErr = "Name is required";
  } else {
    $name = test_input($_POST["name"]);
    // check if name only contains letters and whitespace
    if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
      $nameErr = "Puoi inserire solo lettere";
    }
  }
 
    if (empty($_POST["city"])) {
    $cityErr = "Name is required";
  } else {
    $city = test_input($_POST["city"]);
    // check if name only contains letters and whitespace
    if (!preg_match("/^[a-zA-Z ]*$/",$city)) {
      $cityErr = "Puoi inserire solo lettere";
    }
  }

 
  if (empty($_POST["email"])) {
    $emailErr = "Email is required";
  } else {
    $email = test_input($_POST["email"]);
    // check if e-mail address is well-formed
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
      $emailErr = "Formato email non valido";
    }
  }
    

  if (empty($_POST["bday"])) {
    $bdayErr = "Inserire una data di nascita";
  } else {
    $bday = test_input($_POST["bday"]);
  }
}

function test_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}
?>
    
<div id="content">

<div id="articolo">
        <h1>Registrati</h1>
        
<div class="contenuto-articolo">

<p class="desc" style="text-align:center">Iscriviti alla nostra Newsletter per rimanere sempre aggiornato sulle novità di Buona Strada, partecipare alle inziative del progetto, scoprire tutte le news e gli approfondimenti e molto altro ancora!  </p>

</div>
</div>

<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">


  <div class="container-register">
    <label for="nome"><strong>Nome</strong></label><span class="error">* <?php echo $nameErr;?></span>
    <input type="text" id="nome" placeholder="Inserisci il tuo nome" name="name" value="<?php echo $name;?>" required>
    
    
    <label for="email_form"><strong>Email</strong></label><span class="error">* <?php echo $emailErr;?></span>
    <input type="text" id="email_form" placeholder="Inserisci la tua email" name="email" value="<?php echo $email;?>" required>
    
    <br />
    
    <label for="data"><strong>Data di nascita</strong></label><span class="error">* <?php echo $bdayErr;?></span>
    <input type="date" id="data" name="bday" value="<?php echo $bday;?>">
    
    <br />
    
    <label for="citta"><strong>Città</strong></label> <span class="error">* <?php echo $cityErr;?></span>
    <input type="text" id="citta" placeholder="Inserisci la tua città" name="city" value="<?php echo $city;?>" required>


    <button type="submit">Registrati</button>

  </div>

</form>

        </div>
 
Intanto usi una sola variabile per i vari messaggi di errore e di volta in volta appendi il messaggio. Quindi poco prima di visualizzare di nuovo il form visualizzi il messaggio di errore con un echo ovviamente se c'è un messaggio di errore

Codice:
if(isset($err[0]))
echo "<div>$err</div>";

<form.....
</form>
.
.
.
 
Ciao
potresti provare
PHP:
 if (empty($_POST["bday"])) {
    $bdayErr = "Inserire una data di nascita";
 } else {
    $bday = test_input($_POST["bday"]);
    $bdayok = 1;
 }
$bdayok = 1;
così per ogni controllo e dopo tutti i controlli

PHP:
if($nameok == 1 AND $cityok == 1 AND $emailok == 1 AND $bdayok == 1){
    echo "Registrazione ok";
}else{
    echo "Manca qualcosa";
}
 

Discussioni simili