簡體   English   中英

PHP 表單驗證並在輸入字段旁邊顯示錯誤消息

[英]PHP Form Validation and showing error message beside Input Fields

我試圖在輸入字段之外顯示一條錯誤消息,但我無法這樣做。 我無法找到我在這里犯了什么錯誤。 下面是表單和PHP的代碼。 我的代碼在我看來是正確的,但在瀏覽器中我沒有得到想要的輸出,因為我被困在它上面。 如果有人能幫助我,我將不勝感激。

    <?php

        $Name_Error = "";
        $Email_Error="";
        $Website_Error="";
        $Gender_Error="";

        function Test_User_Input($User_Data){
            return $User_Data;
        }

        if(isset($_POST['Submit'])){
            if(empty($_POST["Name"])){
                $Name_Error = "Kindly Enter the Name!";
            }
            else {
                $Name = Test_User_Input($_POST["Name"]);
            }
            if(empty($_POST["Email"])){
                $Email_Error = "Kindly Enter the Eamil Address!";
            }
            else {
                $Email = Test_User_Input($_POST["Email"]);
            }
            if(empty($_POST["Website"])){
                $Website_Error = "Kindly Enter the Website URL!";
            }
            else {
                $Website = Test_User_Input($_POST["Website"]);
            }
            if(empty($_POST["Gender"])) {
                $Gender_Error = "Kindly Select your Gender!";
            }
            else {
                $Gender = Test_User_Input($_POST["Gender"]);
            }
        }
?>

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Simple Form</title>
</head>
<body>

    <form>
        <label>Enter your Name</label>
        <br>
        <input type="text" name="Name">*<?php echo $Name_Error ?>
        <br>
        <label>Enter your Email Address</label>
        <br>
        <input type="text" name="Email">*<?php echo $Email_Error ?>
        <br>
        <label>Enter your Website</label>
        <br>
        <input type="text" name="Website">*<?php echo $Website_Error ?>
        <br>
        <label>Select your Gender</label>
        <br>
        <input type="radio" name="Gender" value="Male"> Male
        <input type="radio" name="Gender" value="Female">Female *<?php echo $Gender_Error ?>
        <br>
        <label>Comments
        <br>
        <textarea name="Comment"></textarea>
        <br>
        <input type="Submit" name="Submit">
    </form>

</body>
</html>

您需要調用特定頁面來觸發您的 PHP,在這種情況下是頁面本身,方法是 POST 將<form>更改為<form action="" method="POST">

添加表單 action="" 和 method="POST"。 那將解決您的問題。

<?php


$Name_Error = $Email_Error = $Gender_Error = $Website_Error = "";
$Name = $Email = $Gender  = $Website = "";


    if(isset($_POST['Submit'])){
  if (empty($_POST["Name"])) {
    $Name_Error = "Name is required";
  } else {
    $Name = test_input($_POST["Name"]);
  }
  
  if (empty($_POST["Email"])) {
    $Email_Error = "Email is required";
  } else {
    $Email = test_input($_POST["Email"]);
  }
    
  if (empty($_POST["Website"])) {
    $Website_Error = "Kindly Enter the Website URL!";
  } else {
    $Website = test_input($_POST["Website"]);
  }

  if (empty($_POST["Gender"])) {
    $Gender_Error = "Gender is required";
  } else {
    $Gender = test_input($_POST["Gender"]);
  }
}

function test_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}
?>

<h2>PHP Form Validation Example</h2>
<p><span class="error">* required field</span></p>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">  
  Name: <input type="text" name="Name">
  <span class="error">* <?php echo $Name_Error;?></span>
  <br><br>
  E-mail: <input type="text" name="Email">
  <span class="error">* <?php echo $Email_Error;?></span>
  <br><br>
  Website: <input type="text" name="Website">
  <span class="error"><?php echo $Website_Error;?></span>
  <br><br>
  Comment: <textarea name="comment" rows="5" cols="40"></textarea>
  <br><br>
  Gender:
  <input type="radio" name="Gender" value="female">Female
  <input type="radio" name="Gender" value="male">Male
  <input type="radio" name="Gender" value="other">Other
  <span class="error">* <?php echo $Gender_Error;?></span>
  <br><br>
  <input type="submit" name="Submit" value="Submit">  
</form>

<?php
echo "<h2>Your Input:</h2>";
echo $Name;
echo "<br>";
echo $Email;
echo "<br>";
echo $Website;
echo "<br>";
echo $Gender;
?>

</body>
</html>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM