PHP Debugging problem #1 with solution
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
<?php
// define variables and set to empty values
$array_items = [];
$name = $email = $comment = $website = "";
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
$name = test_input($_POST["name"]);
$city = test_input($_POST["city"]);
$month = test_input($_POST["month"]);
$year = test_input($_POST["year"]);
array_push($array_items, "$name", "$city","$month", "$year");
echo "In $array_items[1] in the month of $array_items[2] $array_items[3], you observed
the following weather: <br>" ;
foreach($_POST['weather'] as $item){
echo $item . "\n";
}
function test_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<h2>PHP Form Validation Example</h2>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Weather you experienced : <input type="text" name="name">
City : <input type="text" name="city">
Month : <input type="text" name="month">
Year : <input type="text" name="year">
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
<form action="#" method="post">
<input type="checkbox" name="weather[]" value="rain">rain</input> <br>
<input type="checkbox" name="weather[]" value="sunshine">sunshine</input><br>
<input type="checkbox" name="weather[]" value="clouds">clouds</input><br>
<input type="checkbox" name="weather[]" value="hail">hail</input><br>
<input type="checkbox" name="weather[]" value="sleet">sleet</input><br>
<input type="checkbox" name="weather[]" value="snow">snow</input><br>
<input type="checkbox" name="weather[]" value="wind">wind</input><br>
<input type="submit" name="submit" value="Submit"/>
</form>
</body>
</html>
So the issue here was that that I had declared two separate forms but only one "$_SERVER["REQUEST_METHOD"] == "POST" to cater the post request. So since I wanted both the forms result, I merged both of them in one form.
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Weather you experienced : <input type="text" name="name">
City : <input type="text" name="city">
Month : <input type="text" name="month">
Year : <input type="text" name="year">
<br><br>
<input type="submit" name="submit" value="Submit">
<input type="checkbox" name="weather[]" value="rain">rain</input> <br>
<input type="checkbox" name="weather[]" value="sunshine">sunshine</input><br>
<input type="checkbox" name="weather[]" value="clouds">clouds</input><br>
<input type="checkbox" name="weather[]" value="hail">hail</input><br>
<input type="checkbox" name="weather[]" value="sleet">sleet</input><br>
<input type="checkbox" name="weather[]" value="snow">snow</input><br>
<input type="checkbox" name="weather[]" value="wind">wind</input><br>
<input type="submit" name="submit" value="Submit"/>
</form>