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

1.8k views Asked by At

I created a form using PHP. The form needs to be validated and it works for every field but the "rate our site" drop down menu. I'm using the same code from the form from other questions (name, email, etc.) for the "rate our site" drop down menu but the "* required field" message doesn't appear when the user leaves it blank. I need that error message to appear, like the others, when the user leaves it blank. How can I fix this? Here's my code:

<!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>

Thank you so much for the help everyone! It's greatly appreciated.

3

There are 3 answers

0
isuPatches On BEST ANSWER

The span

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

is inside the select...I bet if you inspected the element it does appear just not visible because it's not in an <option> tag. Try moving it outside.

0
Helmi Joe On

Your error msg should be outside of the <select> scope :

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/>
0
twitch On
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