multiple fruit options

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
chris93
Forum Newbie
Posts: 10
Joined: Fri Aug 01, 2014 4:43 pm

multiple fruit options

Post by chris93 »

here , i have a form where the user can enter their username and adress select multiple fruit options. i cant get it to work.

Code: Select all

<?php
// define variables and initialize with empty values
$nameErr = $addrErr = $emailErr = $howManyErr = $favFruitErr = "";
$name = $address = $email = $howMany = "";
$favFruit = array();
 
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if (empty($_POST["name"])) {
        $nameErr = "Missing";
    }
    else {
        $name = $_POST["name"];
    }
 
    if (empty($_POST["address"])) {
        $addrErr = "Missing";
    }
    else {
        $address = $_POST["address"];
    }
 
    if (empty($_POST["email"]))  {
        $emailErr = "Missing";
    }
    else {
        $email = $_POST["email"];
    }
 
    if (!isset($_POST["howMany"])) {
        $howManyErr = "You must select 1 option";
    }
    else {
        $howMany = $_POST["howMany"];
    }
 
    if (empty($_POST["favFruit"])) {
        $favFruitErr = "You must select 1 or more";
    }
    else {
        $favFruit = $_POST["favFruit"];
    }
}



<input type="text" name="name" value="<?php echo htmlspecialchars($name);?>">
<span class="error"><?php echo $nameErr;?></span>


<input type="text" name="adress" value="<?php echo htmlspecialchars($adress);?>">
<span class="error"><?php echo $addrErr;?></span>


<input type="text" name="email"  value="<?php echo htmlspecialchars($email);?>">
<span class="error"><?php echo $emailErr;?></span>



<input type="radio" name="howMany"
 <?php if (isset($howMany) && $howMany == "zero") echo "checked"; ?>
 value="zero"> 0
<input type="radio" name="howMany"
 <?php if (isset($howMany) && $howMany == "one") echo "checked"; ?>
 value="one"> 1
<input type="radio" name="howMany"
 <?php if (isset($howMany) && $howMany == "two") echo "checked"; ?>
 value="two"> 2
<input type="radio" name="howMany"
 <?php if (isset($howMany) && $howMany == "twoplus") echo "checked"; ?>
 value="twoplus"> More than 2

 <span class="error"><?php echo $howManyErr;?></span>


 
 <select name="favFruit[]" size="4" multiple="">
<?php
$options = array("apple", "banana", "plum", "pomegranate", "strawberry", "watermelon");
foreach ($options as $option) {
    echo '<option value="' . $option . '"';
    if (in_array($option, $favFruit)) {
        echo " selected";
    }
    echo ">" . ucfirst($option) . "</option>";
}
?>
</select>
 
 <span class="error"><?php echo $favFruitErr;?></span>





User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: multiple fruit options

Post by Celauran »

chris93 wrote:i cant get it to work.
Can you elaborate on that? What isn't working? What errors are you seeing?
chris93
Forum Newbie
Posts: 10
Joined: Fri Aug 01, 2014 4:43 pm

Re: multiple fruit options

Post by chris93 »

all im getting is


Parse error: syntax error, unexpected '<' in C:\xampp2\htdocs\tutorials\forms\php-form-validator\samples\1-simple-example\options.php on line 46
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: multiple fruit options

Post by Celauran »

You need to close your PHP tag after your if statements.

Code: Select all

<?php // Just here for syntax highlighting...
    if (empty($_POST["favFruit"])) {
        $favFruitErr = "You must select 1 or more";
    }
    else {
        $favFruit = $_POST["favFruit"];
    }
}

?>

<input type="text" name="name" value="<?php echo htmlspecialchars($name);?>">
<span class="error"><?php echo $nameErr;?></span>
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: multiple fruit options

Post by Celauran »

If you're not already using one, get yourself and editor or IDE that does syntax highlighting. These things will jump out at you.
Post Reply