I am using input from a html form to output arrays containing the value inputted. I would like to carry out basic error handling that states what fields were left empty, a error message if no fields are inputted and then if all of the fields are inputed. However, my code isn't doing as I would like. It works elsewhere in my code but I can not figure out the difference.
EDIT: I am getting "Notice: Undefined index: ageChoice" and "Notice: Undefined index: colourChoice"
<form action="profileFilter.php" method="POST">
<p>Age: <br>
<input type="radio" name="ageChoice" value="1">1
<input type="radio" name="ageChoice" value="2">2
<p>Colour: <br>
<input type="radio" name="colourChoice" value="Tabby">Tabby:
<input type="radio" name="colourChoice" value="Ginger">Ginger
<input type="submit" name="button">
</form>
class ProfileArray {
Public function dataArray() {
$profileArray = array(
array( 'Name' => 'Toby',
'Age' => 3,
'Colour' => 'Ginger',
'Gender' => 'Male',
'Personality' => 'Gentle',
),
array( 'Name' => 'Cassie',
'Age' => 2,
'Colour' => 'Tabby',
'Gender' => 'Female',
'Personality' => 'Playful',
),
);
return $profileArray;
}
}
include ("ProfileArray.class.php");
$object = new ProfileArray();
$profileArray = $object->dataArray();
if($_SERVER["REQUEST_METHOD"] == "POST") {
$passedAgeChoice = $_POST['ageChoice'];
$passedcolourChoice = $_POST['colourChoice'];
$errorMessage = "";
if (!empty($passedAgeChoice)) {
echo $errorMessage = "WARNING: You have not entered an age to search for<br>";
}
if (!empty($passedColourChoice)) {
echo $errorMessage = "WARNING: You have not entered a colour to search for<br>";
}
if ($errorMessage == TRUE) {
echo '<h4 class="error">You have inputted nothing to search for</h4><a href="catMatch.html" class="error_button"> Go back and try again</a>';
}
//If there are no errors submit the form
if ($errorMessage == "") {
echo '<h4 class="error">Here are your full search results</h4><a href="catMatch.html" class="error_button"> Back to Homepage</a><br>';
}
}
Use isset()
and !isset()
for everything here and you won't get any warnings. As I mentioned in comments, isset()
is best used for radio buttons (and checkboxes).
empty()
is mostly used for string inputs, lengths etc.Sidenote: $passedcolourChoice
has also been replaced with $passedColourChoice
.
Variables are case-sensitive.
You can omit $passedAgeChoice = $_POST['ageChoice']; $passedcolourChoice = $_POST['colourChoice'];
and use the below:
if(isset($_POST['ageChoice'])){
$passedAgeChoice = $_POST['ageChoice'];
}
if(isset($_POST['colourChoice'])){
$passedColourChoice = $_POST['colourChoice'];
}
$errorMessage = "";
if (!isset($passedAgeChoice)) {
echo $errorMessage = "WARNING: You have not entered an age to search for<br>";
}
if (!isset($passedColourChoice)) {
echo $errorMessage = "WARNING: You have not entered a colour to search for<br>";
}
if ($errorMessage == TRUE) {
echo '<h4 class="error">You have inputted nothing to search for</h4><a href="catMatch.html" class="error_button"> Go back and try again</a>';
}
In profileFilter.php change two lines
$passedAgeChoice = (isset ($_POST['ageChoice'])) ?$_POST['ageChoice']:'';
$passedcolourChoice =(isset ($_POST['clourChoice'])) ? $_POST['colourChoice']:'';
Best practice: don't use global variables (e.g. $_POST, $_GET) directly. Try to validate them before use.