Reputation: 155
I'm looking for a bit of help and a bit of an explanation here.
I have created an HTML form with several input fields and some very basic PHP validation when POSTing the inputs. My validation just checks to see if the field has data and if not, it prompts the user to enter data in the field by displaying an error. My hope is ultimately to POST these inputs, check them against a database, and if they aren't there, then add them to the database. But, this is not my issue at hand.
Currently, my objective is to take all of the inputs in my field that I want to POST and to display them in a field below my error display area. I had hoped to just echo the data but for some reason, not all of the entered data appears.
Of the 5 entry fields in the code below, 4 are basic input fields and one is a text area. If i enter anything into the basic input fields, only the last input will echo in my display area. If i enter something into field 1 and leave the rest blank, field 1 will display. Also, if I enter something into the text area, it will always display. Finally, my PHP validation does not appear to work with my textarea type input (labeled 'note') and will not return an error if the 'note' input is left blank.Can anyone explain: (1) How do I fix it so that all 5 inputs display in the display div?; (2) Why does this happen?; (3) why is no error returned if the text area (labeled 'note') is left blank?
Thank you.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<?php
//form validation for general entry form
// define variables and set to empty values
$clientErr = $matterErr = $dateErr = $timeErr = $noteErr= "";
$client = $matter = $date = $time = $note = "";
//on post, check to see if variable is empty. if not empty
//parse it and assign value back to variable name
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$complete = true;
$postArray = [];
if (empty($_POST["client"])) {
$clientErr = "*A client name is required. ";
$complete = false;
}else {
$client = test_input($_POST["client"]);
//$postArray[] = $client;
}
if (empty($_POST["matter"])) {
$matterErr = "*A matter name is required. ";
$complete = false;
}else {
$client = test_input($_POST["matter"]);
}
if (empty($_POST["date"])) {
$dateErr = "*A date is required. ";
$complete = false;
}else {
$client = test_input($_POST["date"]);
}
if (empty($_POST["time"])) {
$timeErr = "*A time entry is required. ";
$complete = false;
}else {
$client = test_input($_POST["time"]);
}
if (empty($_POST["note"])) {
$noteErr = "*A note is required. ";
$complete = false;
} else {
$note = test_input($_POST["note"]);
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" >
<label for="client">Client:</label>
<input type="text" placeholder = "Enter Client Name" name="client"> *
<label for="matter">Matter:</label>
<input type="text" placeholder = "Enter Matter Name" name="matter"> *
<label for="date">Date:</label>
<input type="text" placeholder = "Enter Date" name="date"> *
<label for="time">Time:</label>
<input type="text" placeholder ="Time to nearest tenth hour" name="time"> *
<label for="note">Note:</label>
<textarea name="note" placeholder ="Enter Any Notes" rows="4" cols="40"></textarea>
<input type="submit" name="submit" value="Submit" class="submitbutton">
</form>
<div class="errorDiv">
<?php
echo $clientErr;
echo $matterErr;
echo $dateErr;
echo $timeErr;
?>
</div>
<div class ="displayDiv">
<?php
echo "<h2>Your Input:</h2>";
echo $client;
echo "<br>";
echo $matter;
echo "<br>";
echo $date;
echo "<br>";
echo $time;
echo "<br>";
echo $note;
?>
</div>
</body>
</html>
Upvotes: 0
Views: 1157
Reputation: 2785
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<?php
//form validation for general entry form
// define variables and set to empty values
$clientErr = $matterErr = $dateErr = $timeErr = $noteErr= "";
$client = $matter = $date = $time = $note = "";
//on post, check to see if variable is empty. if not empty
//parse it and assign value back to variable name
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$complete = true;
$postArray = [];
if (empty($_POST["client"])) {
$clientErr = "*A client name is required. ";
$complete = false;
}else {
$client = test_input($_POST["client"]);
//$postArray[] = $client;
}
if (empty($_POST["matter"])) {
$matterErr = "*A matter name is required. ";
$complete = false;
}else {
$matter = test_input($_POST["matter"]); //remove $client and assign value to $matter
}
if (empty($_POST["date"])) {
$dateErr = "*A date is required. ";
$complete = false;
}else {
$date = test_input($_POST["date"]); //remove $client and assign value to $date
}
if (empty($_POST["time"])) {
$timeErr = "*A time entry is required. ";
$complete = false;
}else {
$time = test_input($_POST["time"]); //remove $client and assign value to $time
}
if (empty($_POST["note"])) {
$noteErr = "*A note is required. ";
$complete = false;
} else {
$note = test_input($_POST["note"]);
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" >
<label for="client">Client:</label>
<input type="text" placeholder = "Enter Client Name" name="client"> *
<label for="matter">Matter:</label>
<input type="text" placeholder = "Enter Matter Name" name="matter"> *
<label for="date">Date:</label>
<input type="text" placeholder = "Enter Date" name="date"> *
<label for="time">Time:</label>
<input type="text" placeholder ="Time to nearest tenth hour" name="time"> *
<label for="note">Note:</label>
<textarea name="note" placeholder ="Enter Any Notes" rows="4" cols="40"></textarea>
<input type="submit" name="submit" value="Submit" class="submitbutton">
</form>
<div class="errorDiv">
<?php
echo $clientErr;
echo $matterErr;
echo $dateErr;
echo $timeErr;
echo $noteErr; // echo noteerror here.........
?>
</div>
<div class ="displayDiv">
<?php
echo "<h2>Your Input:</h2>";
echo $client;
echo "<br>";
echo $matter;
echo "<br>";
echo $date;
echo "<br>";
echo $time;
echo "<br>";
echo $note;
?>
</div>
</body>
</html>
Here you assign all values to single variable $client
and echo all different variable so assign value to particular variable like $matter
, $date
, $time
. And you forget to echo $noteerror
in error messsages.
Upvotes: 5
Reputation: 1861
Why are you assigning all the values to same $client variable? It has to be replaced with different variables.
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$complete = true;
$postArray = [];
if (empty($_POST["client"])) {
$clientErr = "*A client name is required. ";
$complete = false;
}else {
$client = test_input($_POST["client"]);
//$postArray[] = $client;
}
if (empty($_POST["matter"])) {
$matterErr = "*A matter name is required. ";
$complete = false;
}else {
$matter= test_input($_POST["matter"]);
}
if (empty($_POST["date"])) {
$dateErr = "*A date is required. ";
$complete = false;
}else {
$date= test_input($_POST["date"]);
}
if (empty($_POST["time"])) {
$timeErr = "*A time entry is required. ";
$complete = false;
}else {
$time= test_input($_POST["time"]);
}
if (empty($_POST["note"])) {
$noteErr = "*A note is required. ";
$complete = false;
} else {
$note = test_input($_POST["note"]);
}
Upvotes: 2
Reputation: 3630
You copy and pasted your else
block, didn't you? In each block you assign the value to $client
and thereby overwrite it with each new $_POST
value. Change the other assignments to $client
to $time
or appropriate and try that.
For example this
$client = test_input($_POST["date"]);
should probably be
$date = test_input($_POST["date"]);
Upvotes: 4