Wenn bei der Formularvalidierung ein Problem auftritt, geben Sie bitte das Problem an (PHP-Validierung).
P粉268284930
P粉268284930 2023-09-09 19:34:57
0
1
546

Ich möchte wissen, was mit dem Formular in meinem Code nicht stimmt.

<?php

if(isset($_POST['submit'])) {
  $arrayName = array("TeacherA", "TeacherB", "TeacherC" , "TeacherD", "TeacherE");

  $minimum = 5;
  $maximum = 10;

  $name = $_POST['yourName'];
  $email = $_POST['yourEmail'];

  if(strlen($name) < $minimum) {
    echo "Your name should be longer than 5 characters";
  } 
  
  if(strlen($name) > $maximum) {
    echo "Your name should not be longer than 10 characters";
  } 
  
  if(!in_array($name,$arrayName)){
    echo "Please do register with us before you can login";
  } else {
    echo "Welcome!";
  }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Blank</title>
</head>
<body>
  <form action="form.php" method="post">
    <label>Your Name:    </label>
    <input type="text" name="yourName"> <br>

    <label for="">Your E mail:</label>
    <input type="email" name="yourEmail" id=""><br>
    
    <!-- <textarea name="yourMessage" id="" cols="30" rows="10"></textarea><br> -->
    <input type="submit" name="submit" value="submit">
  </form>
</body>
</html>

Ich habe den Code lokal ausgeführt, konnte aber nicht die gewünschten Ergebnisse erzielen.

Das Ergebnis, das ich möchte, ist, dass, wenn ich keinen Namen in das Feld „Ihr Name“ eingebe, das Ergebnis angezeigt werden sollte:

Bitte registrieren Sie sich, bevor Sie sich anmelden

P粉268284930
P粉268284930

Antworte allen(1)
P粉232793765

如果“Your Name”字段为空,则其strlen($name)应为0,第一个if语句为真,并显示Your name should be longer than 5 characters 你可以尝试这个:

<?php   
if (isset($_POST['submit'])) {
    $arrayName = array("TeacherA", "TeacherB", "TeacherC", "TeacherD", "TeacherE");
    $minimum = 5;
    $maximum = 10;
    $name = $_POST['yourName'];
    $email = $_POST['yourEmail'];
    if (empty($name)) {
        echo "Please do register with us before you can login";
    } else {
        if (strlen($name) < $minimum) {
            echo "Your name should be longer than 5 characters";
        } else {
            if (strlen($name) > $maximum) {
                echo "Your name should be less than 10 characters";
            } else {
                if (in_array($name, $arrayName)) {
                    echo "Welcome!";
                } else {
                    echo "Your login is not correct";
                }
            }
        }
    }
}
?>

i used empty() to test if the field empty and i used if-else statements because i want the script to stop if it founds a 'true' condition in your script you can use return; but that will exit the rest of your script

have a nice code :)

Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage