Reputation: 159
I am fairly new at PHP. I have an assignment which requires me to make two separate files. On called input.html and the other handle.php.
The input.html file has a for that will receive user input and then send it to the handle.php file.
The input.html should look like
and if all the fields are filed in then I should display this
The complication that I'm having is with the required filed. If the fields do not have text or the radio is not checked then it should warn users but with me it keep sending them to the PHP file and showing an error.
My code for input.html is
<!doctype html>
<html lang="en">
<head>
<title> Feedback Form </title>
<style>
.error {color: #FF0000;}
</style>
</head>
<body>
<?php
// define variables and set to empty values
$nameErr = $emailErr = $responseErr = $commentErr = "";
$name = $email = $response = $comment = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
$nameErr = "Name is required";
} else {
$name = test_input($_POST["name"]);
//Check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$name)){
$nameErr = "Only letters and white space allowed";
}
}
if (empty($_POST["email"])) {
$emailErr = "Email is required";
} else {
$email = test_input($_POST["email"]);
//Check if email address is well-formated
if (!filter_var($email, FILTER_VALIDATE_EMAIL)){
$emailErr = "Invalid email format";
}
}
if (empty($_POST["response"])) {
$responseErr = "Response is required";
} else {
$response = test_input($_POST["response"]);
}
if (empty($_POST["comment"])) {
$commentErr = "Comment is required";
} else {
$comment = test_input($_POST["comment"]);
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<p>Please complete this form to submit your feedback:</p>
<p><span class="error">* required field</span></p>
<!-- Start of the form -->
<form method="post" action="handle.php">
Name: <select name="prefix">
<option>Mr. </option>
<option>Mrs. </option>
<option>Ms. </option>
</select> <input type="text" name="name">
<span class="error">* <?php echo $nameErr;?></span>
<br><br>
Email Address: <input type="text" name="email">
<span class="error">* <?php echo $emailErr;?></span>
<br><br>
Response: This is...
<input type="radio" name="response" value="excellent">excellent
<input type="radio" name="response" value="okay">okay
<input type="radio" name="response" value="boring">boring
<span class="error">* <?php echo $responseErr;?></span>
<br><br>
Comments: <textarea name="comment" rows="5" cols="40"></textarea>
<span class="error">* <?php echo $commentErr;?></span>
<br><br>
<input type="submit" value="Send My Feedback">
</form>
<!-- End of the form -->
</body>
</html>
and my code for handle.php is
<html>
<head></head>
<body>
Thank you, <?php echo $_POST["prefix"] ?> <?php echo $_POST["name"]; ?> for your comment.<br><br>
You stated that you found this example to be '<?php echo $_POST["response"]; ?>'<br>
and added: <br>
'<?php echo $_POST['comment'];?>'<br>
</body>
</html>
So when I leave all the fields blank instead of it warning me that they are blank i get this
I tried to use w3shool as a guide line but it did not work. I would really appreciate some help and thank you to everything that is taking their time to attempt to help me. :)
Upvotes: 0
Views: 30
Reputation: 1018
You can use required
to solve your problem. required
stops the form
from submitting if the required field is empty or not selected.
<form method="post" action="handle.php">
Name:
<select name="prefix">
<option>Mr. </option>
<option>Mrs. </option>
<option>Ms. </option>
</select>
<input type="text" name="name" required>
<span class="error">* <?php echo $nameErr;?></span>
<br>
<br> Email Address:
<input type="email" name="email" required>
<span class="error">* <?php echo $emailErr;?></span>
<br>
<br> Response: This is...
<input type="radio" name="response" value="excellent" required>excellent
<input type="radio" name="response" value="okay" required>okay
<input type="radio" name="response" value="boring" required>boring
<span class="error">* <?php echo $responseErr;?></span>
<br>
<br> Comments:
<textarea name="comment" rows="5" cols="40" required></textarea>
<span class="error">* <?php echo $commentErr;?></span>
<br>
<br>
<input type="submit" value="Send My Feedback">
</form>
I also recommend to use 'email' as type for email input field. This will make sure the input data is a valid email format.
Upvotes: 1