0

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.

  • There are no valid emails that will make it past this condition `preg_match("/\w+/",$_POST['email'])`. To check if a string is all alphabetical characters `ctype_alpha()`, to check if alphanumeric `ctype_alnum()`, to check if all numeric `ctype_digit()`. If you want to pile errors into a single "thing", that thing is an array. – mickmackusa Mar 08 '21 at 04:46
  • what I'm trying to do there is brute force the code to satisfy that condition if there's actual content, because what it's doing is giving me the success message with empty variables. anything can go in those fields really, as long as it's not empty. – thundafellow Mar 08 '21 at 04:48
  • You are missing curly braces on your conditions. Therefore only the next line of code is executed (not the incrementation of `$filled`. – mickmackusa Mar 08 '21 at 05:00
  • Holy cow thank you! I thought those were fine because I thought that was shorthand. Turns out I'm wrong. Go ahead and make an official answer to the question so I can mark it solved! Seriously thank you so much! – thundafellow Mar 08 '21 at 05:05
  • that's not how it works here. If a new question can be resolved by pointing the asker to a pre-existing question, we are only meant to close the page as a duplicate and not answer. This page will eventually be removed by a Stack Overflow script. – mickmackusa Mar 08 '21 at 05:23
  • Also, there is absolutely no benefit in repeating an option's text as the option's value. `echo "";` should only be `echo ""; The `selected` attribute should be conditionally added within your `foreach()` loop. `else if` should be one word in php -- `elseif`. – mickmackusa Mar 08 '21 at 05:24

0 Answers0