Il miglior tutorial PHP Forme - Campi obbligatori Nel 2024, in questo tutorial puoi imparare Campi obbligatori - PHP,PHP - messaggio di errore,

PHP Forme - Campi obbligatori

Questa sezione descrive come impostare i campi del modulo richiesti e messaggi di errore.


Campi obbligatori - PHP

Nell'ultimo capitolo abbiamo introdotto le regole di validazione della tabella, possiamo vedere il "Nome", "E-mail", e il campo "sesso" è necessario, ogni campo non può essere vuoto.

字段 验证规则
名字 必需。 + 只能包含字母和空格
E-mail 必需。 + 必需包含一个有效的电子邮件地址(包含"@"和".")
网址 可选。 如果存在,它必需包含一个有效的URL
备注 可选。多行字段(文本域)。
性别 必需。必需选择一个。

Se nel capitolo precedente, tutti i campi di input sono opzionali.

Nel codice seguente abbiamo aggiunto alcune nuove variabili: $ nameErr, $ emailErr, $ genderErr, e $ websiteErr it Questi errori vengono visualizzati sulle variabili campi richiesti. Abbiamo anche aggiunto un altro if per ogni variabile $ _POST. Queste dichiarazioni saranno verificare se la variabile $ _POST è vuoto (utilizzando la funzione vuoto di PHP ()). Se vuoto, viene visualizzato il messaggio di errore corrispondente. Se non è vuota, i dati saranno comunicati al test_input function ():

<?php
// 定义变量并默认设为空值
$nameErr = $emailErr = $genderErr = $websiteErr = "";
$name = $email = $gender = $comment = $website = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
  if (empty($_POST["name"])) {
    $nameErr = "名字是必需的。";
  } else {
    $name = test_input($_POST["name"]);
  }

  if (empty($_POST["email"])) {
    $emailErr = "邮箱是必需的。";
  } else {
    $email = test_input($_POST["email"]);
  }

  if (empty($_POST["website"])) {
    $website = "";
  } else {
    $website = test_input($_POST["website"]);
  }

  if (empty($_POST["comment"])) {
    $comment = "";
  } else {
    $comment = test_input($_POST["comment"]);
  }

  if (empty($_POST["gender"])) {
    $genderErr = "性别是必需的。";
  } else {
    $gender = test_input($_POST["gender"]);
  }
}
?>

PHP - messaggio di errore

Nei seguenti esempi di form HTML, abbiamo aggiunto alcuni script per ogni campo, ogni script verrà visualizzato un messaggio di errore quando l'errore di input informazioni. (Se l'utente non compila le informazioni e inviare il modulo di esso emette un messaggio di errore):

<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> 
   名字: <input type="text" name="name">
   <span class="error">* <?php echo $nameErr;?></span>
   <br><br>
   E-mail: <input type="text" name="email">
   <span class="error">* <?php echo $emailErr;?></span>
   <br><br>
   网址: <input type="text" name="website">
   <span class="error"><?php echo $websiteErr;?></span>
   <br><br>
   备注: <textarea name="comment" rows="5" cols="40"></textarea>
   <br><br>
   性别:
   <input type="radio" name="gender" value="female">女
   <input type="radio" name="gender" value="male">男
   <span class="error">* <?php echo $genderErr;?></span>
   <br><br>
   <input type="submit" name="submit" value="Submit"> 
</form>

Visualizza codice >>

PHP Forme - Campi obbligatori
10/30