我正在尝试在输入字段之外显示错误消息,但我无法这样做。我找不到在这里犯的错误。下面是表单和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>
答案 0 :(得分:1)
您需要调用特定页面来触发您的PHP,在这种情况下,它是页面本身,方法是POST
将<form>
更改为<form action="" method="POST">
答案 1 :(得分:0)
添加表单 action="" 和 method="POST"。这将解决您的问题。
答案 2 :(得分:0)
<?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>