如何在验证 PHP 表单中为下拉菜单显示“*必填字段”错误消息?

How can I make a "*required field" error message appear for a drop down menu in a validating PHP form?

我使用 PHP 创建了一个表单。表单需要验证,它适用于每个字段,但 "rate our site" 下拉菜单除外。我在 "rate our site" 下拉菜单中使用其他问题(姓名、电子邮件等)表单中的相同代码,但当用户将其留空时,不会出现“* 必填字段”消息。当用户将其留空时,我需要像其他错误消息一样显示该错误消息。我怎样才能解决这个问题?这是我的代码:

<!DOCTYPE HTML> 
<html>
<head>
<title>Roy Feedback Form Assignment 7</title>
<style> .error {color: #FF0000;} </style>
</head>
<body> 

<?php
// define variables and set to empty values
$nameErr = $emailErr = $commentErr = $likesErr = $howErr = $rateErr = "";
$name = $email = $comment = $likes = $how = $rate = "";


if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
 $nameErr = "Name is required";
} else {
 $name = test_input($_POST["name"]);
}

if (empty($_POST["email"])) {
 $emailErr = "Email is required";
} else {
 $email = test_input($_POST["email"]);
}

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

if (empty($_POST["likes"])) {
 $likesErr = "Things you liked is required";
} else {
 $likes = test_input($_POST["likes"]);
}

if (empty($_POST["how"])) {
 $howErr = "How you got to our site is required";
} else {
 $how = test_input($_POST["how"]);
}

if (empty($_POST["rate"])) {
 $rateErr = "Rating our site is required";
} else {
 $rate = test_input($_POST["rate"]);
}
}

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

<h2>Roy Feedback Form Assignment 7</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 $nameErr;?></span>
<br><br>

E-mail: <input type="text" name="email">
<span class="error">* <?php echo $emailErr;?></span>
<br><br>

Comment: <textarea name="comment" rows="5" cols="40"></textarea>
<span class="error">* <?php echo $commentErr;?></span>
<br><br>

Things you liked:
<input type="radio" name="likes" value="Site design">Site design
<input type="radio" name="likes" value="Links">Links
<input type="radio" name="likes" value="Ease of use">Ease of use
<input type="radio" name="likes" value="Images">Images
<input type="radio" name="likes" value="Source code">Source code
<span class="error">* <?php echo $likesErr;?></span>
<br><br>

How you got to our site:
<input type="radio" name="how" value="Search engine">Search engine
<input type="radio" name="how" value="Links from another site">Links from another site
<input type="radio" name="how" value="Deitel.com website">Deitel.com website
<input type="radio" name="how" value="Reference from a book">Reference from a book
<input type="radio" name="how" value="Other">Other
<span class="error">* <?php echo $howErr;?></span>
<br><br>

Rate our site:
<select name="rate">
<option value="">- Please Select -</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<span class="error">* <?php echo $rateErr;?></span>
</select>
<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 $comment;
echo "<br>";
echo $likes;
echo "<br>";
echo $how;
echo "<br>";
echo $rate;
?>

</body>
</html>

非常感谢大家的帮助!非常感谢。

跨度

<span class="error">* <?php echo $rateErr;?></span>

位于 select 内...我敢打赌,如果您检查了该元素,它确实看起来不可见,因为它不在 <option> 标记中。试着把它移到外面。

您的错误消息应该在 <select> 范围之外:

Rate our site:
<select name="rate">
<option value="">- Please Select -</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select>
<span class="error">* <?php echo $rateErr;?></span>
<br/><br/>
HTML5 allows you to validate a form like in your case

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

Name: <input type="text" name="name" required>
<span class="error">*</span>
<br><br>

E-mail: <input type="email" name="email" required>
<span class="error">*</span>
<br><br>

Comment: <textarea name="comment" rows="5" cols="40" required></textarea>
<span class="error">*</span>
<br><br>

Things you liked:
<input type="radio" name="likes" value="Site design" required>Site design
<input type="radio" name="likes" value="Links" required>Links
<input type="radio" name="likes" value="Ease of use" required>Ease of use
<input type="radio" name="likes" value="Images" required>Images
<input type="radio" name="likes" value="Source code" required>Source code
<span class="error">*</span>
<br><br>

How you got to our site:
<input type="radio" name="how" value="Search engine" required>Search engine
<input type="radio" name="how" value="Links from another site" required>Links from another site
<input type="radio" name="how" value="Deitel.com website" required>Deitel.com website
<input type="radio" name="how" value="Reference from a book" required>Reference from a book
<input type="radio" name="how" value="Other" required>Other
<span class="error">*</span>
<br><br>

Rate our site:
<select name="rate" required>
<option value="">- Please Select -</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<span class="error">*</span>
</select>
<br/><br/>

<input type="submit" name="submit" value="Submit"> 

</form> This will automatically check on-submit form