I am working on an assignment that needs me to process a band merch order using dropdowns and text entry. I am processing the php on the same page. I need to fill a requirement that asks for an error to be displayed when one or more of the fields are not filled in. I am having a lot of trouble with the logic and am looking for someone to look over my code and see if they can spot anything. I have searched Stack Overflow literally all day and can't find any solution. It should be like this: page loads, form is up, no messages. You make your selections and enter your information and hit submit. If one or more fields is empty, it displays a message saying to fill in all of the fields. If all fields are filled in, it displays the information you entered and tells you what kind of merch you ordered. Right now it's doing this: page loads with the "fill in all fields" error. I make my selection and fill in all fields, and it works. However, if I hit submit without entering anything, it gives me the success message but with no data, just whitespace. No sign of the error.
Here is my code:
<?php
$message = "";
$chosenBand = $chosenColor = $chosenSize = $chosenStyle = $firstName = $lastName = $emailAddress =
$phoneNumber = $streetAddress = $cityName = $stateName = $zipCode = "";
$filled = 0;
if(isset($_POST['Submit']))
{
$filled = 0;
if(isset($_POST['bands']) && preg_match("/\w+/",$_POST['bands']) && $_POST['bands'] != "Choose a Band")
$chosenBand = $_POST['bands'];
$filled++;
if(isset($_POST['color']) && preg_match("/\w+/",$_POST['color']) && $_POST['color'] != "Choose a Color")
$chosenColor = $_POST['color'];
$filled++;
if(isset($_POST['size']) && preg_match("/\w+/",$_POST['size']) && $_POST['size'] != "Choose a Size")
$chosenSize = $_POST['size'];
$filled++;
if(isset($_POST['style']) && preg_match("/\w+/",$_POST['style']) && $_POST['style'] != "Choose a Style")
$chosenStyle = $_POST['style'];
$filled++;
if(isset($_POST['fname']) && preg_match("/\w+/",$_POST['fname']))
$firstName = $_POST['fname'];
$filled++;
if(isset($_POST['lname']) && preg_match("/\w+/",$_POST['lname']))
$lastName = $_POST['lname'];
$filled++;
if(isset($_POST['email']) && preg_match("/\w+/",$_POST['email']))
$emailAddress = $_POST['email'];
$filled++;
if(isset($_POST['phone']) && preg_match("/\w+/",$_POST['phone']))
$phoneNumber = $_POST['phone'];
$filled++;
if(isset($_POST['street']) && preg_match("/\w+/",$_POST['street']))
$streetAddress = $_POST['street'];
$filled++;
if(isset($_POST['city']) && preg_match("/\w+/",$_POST['city']))
$cityName = $_POST['city'];
$filled++;
if(isset($_POST['state']) && preg_match("/\w+/",$_POST['state']))
$stateName = $_POST['state'];
$filled++;
if(isset($_POST['zip']) && preg_match("/\w+/",$_POST['zip']))
$zipCode = $_POST['zip'];
$filled++;
if($filled == 12)
{
$message = $firstName." ".$lastName."<br>".$streetAddress."<br>".$cityName.", ".$stateName." ".$zipCode."<br>".$emailAddress."<br>".$phoneNumber."<br>"."Ordered: 1 ".$chosenSize." ".$chosenColor." ".$chosenBand." ".$chosenStyle;
}
else if($filled < 12)
{
$message = "Please fill out all fields";
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Products</title>
</head>
<body>
<h1>Products page</h1>
<form method="post">
<select name="bands" id="bands">
<option selected="selected"><?php echo $result = isset($_POST['Submit']) ? $chosenBand : 'Choose a Band';?></option>
<?php
$bandNames = array("Brand New", "Manchester Orchestra", "Twenty One Pilots", "Chequerboard",
"The Glitch Mob", "Olafur Arnalds", "Patrick O'Hearn", "Madeon", "Her Name is Calla", "pg.lost");
foreach($bandNames as $name)
{
echo "<option value='$name'>$name</option>";
}
?>
</select>
<select name="color" id="color">
<option selected="selected"><?php echo $result = isset($_POST['Submit']) ? $chosenColor : 'Choose a Color';?></option>
<?php
$colors = array("Black", "Gray", "White", "Blue", "Red", "Pink");
foreach($colors as $color)
{
echo "<option value='$color'>$color</option>";
}
?>
</select>
<select name="size" id="size">
<option selected="selected"><?php echo $result = isset($_POST['Submit']) ? $chosenSize : 'Choose a Size';?></option>
<?php
$sizes = array("XS", "S", "M", "L", "XL", "XXL");
foreach($sizes as $size)
{
echo "<option value='$size'>$size</option>";
}
?>
</select>
<select name="style" id="style">
<option selected="selected"><?php echo $result = isset($_POST['Submit']) ? $chosenStyle : 'Choose a Style';?></option>
<?php
$styles = array("Crew Neck Tee", "Crew Neck Sweatshirt", "Hoodie", "Crop Top", "Bomber Jacket",
"Windbreaker");
foreach($styles as $style)
{
echo "<option value='$style'>$style</option>";
}
?>
</select><br><br>
<input value="<?php echo $firstName;?>" placeholder="First Name" name="fname" id="fname"><br>
<input value="<?php echo $lastName;?>" placeholder="Last Name" name="lname" id="lname"><br>
<input value="<?php echo $emailAddress;?>" placeholder="Email" name="email" id="email"><br>
<input value="<?php echo $phoneNumber;?>" placeholder="Phone" name="phone" id="phone"><br><br>
<input value="<?php echo $streetAddress;?>" placeholder="Street" name="street" id="street"><br>
<input value="<?php echo $cityName;?>" placeholder="City" name="city" id="city"><br>
<input value="<?php echo $stateName;?>" placeholder="State" name="state" id="state"><br>
<input value="<?php echo $zipCode;?>" placeholder="Zip" name="zip" id="zip"><br><br>
<input type="submit" value="Submit" name="Submit"><br><br>
<?php echo $message; ?>
<?php echo $filled; ?>
</form>
</body>
</html>
I am very new to PHP so please keep that in mind.