jeudi 29 juin 2017

PHP If statement on select elemnet created with array

I'm new to PHP and encountering an issue I am unable to resolve.

say we have this:

<select name="car">
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="mercedes">Mercedes</option>
  <option value="audi">Audi</option>
</select>

and my php is:

if (isset($_POST['car']) && $_POST['car'] == "Audi") {
    echo 'Please select a better car.';
}

this works. However,I made a generated select off an array:

<select name="q1" value="<?php echo $q1 ?>">
        <?php foreach ($toppings as $key => $value) { ?>
            <option <?php if ($q1 == $key) { ?>selected="true" <?php }; ?>value="<?php echo $key ?>"><?php echo $value ?></option>
        <?php } ?>
    </select>

and then I wrote this php code as follows:

<?php
$toppings = array(1 => "Anchovie", 2 => "Tomato", 3 => "Corn", 4 => "Bulgarian", 5 => "Pineapple", 6 => "Pepperoni", 7 => "Green Olives", 8 => "Ground Beef", 9 => "Red Peppers", 10 => "Tuna", 11 => "Cheese");

if (isset($_POST['q1']) && $_POST['q1'] == "Cheese") {
    echo 'you selected cheese!.';
}
?>

This does not work and I am unable to figure why on my own. Please help, thank you.

Aucun commentaire:

Enregistrer un commentaire