1 year ago

#218910

test-img

Joel

Submit only sends the result of one option group

I'm really beggining with php but have a little of experience programming in C. So was trying to make two labels options to add one of each category, finally show the results with a submit button. But I just get one result, if I don't choose nothing at all in the second category, it gives only the result of the first category, and vice versa. My objective is to get the results of both selections of the two categories. BTW, I'm running this page with XAMPP and working in the code with VS Code.

Here is the code

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Buying a lunch</title>
</head>
<body>
    <div class="container mt-2">
        <?php
            $apple = 2;
            $banana = 3;
            $pineapple = 5;
            $watermelon = 8;
            $grapes = 6;

            $cocacola = 8;
            $mountaindew = 10;
            $pepsi = 6;
            $fanta = 7;
            $water = 3;
            
            $total = 0;
        ?>


        <div class="row">
            <div class="col-12">
                <form>
                    <div class="form-group">
                        <label>Something to eat</label>
                        <?php $fruits=$_GET['fruits']??''; ?>
                        <select name="fruits" class="form-control">
                            <option value="" <?php if($fruits=='')echo "selected='selected'"; ?> >Choose a fruit</option>
                                <option value="apple"<?php if($fruits=='apple')echo "selected='selected'"; ?>>An apple</option>
                                <option value="banana"<?php if($fruits=='banana')echo "selected='selected'"; ?>>An banana</option>
                                <option value="pineapple"<?php if($fruits=='pineapple')echo "selected='selected'"; ?>>An pineapple</option>
                                <option value="watermelon"<?php if($fruits=='watermelon')echo "selected='selected'"; ?>>An watermelon</option>
                                <option value="grapes"<?php if($fruits=='grapes')echo "selected='selected'"; ?>>An grapes</option>

                        </select>
                    </div>

                    <div class="form-group">
                        <label>Something to drink</label>
                        <?php $drinks=$_GET['drinks']??''; ?>
                        <select name="drinks" class="form-control">
                            <option value="" <?php if($drinks=='')echo "selected='selected'"; ?> >Choose a drink</option>
                                <option value="cocacola"<?php if($drinks=='cocacola')echo "selected='selected'"; ?>>A Cocacola</option>
                                <option value="mountaindew"<?php if($drinks=='mountaindew')echo "selected='selected'"; ?>>A MountainDew</option>
                                <option value="pepsi"<?php if($drinks=='pepsi')echo "selected='selected'"; ?>>A Pepsi</option>
                                <option value="fanta"<?php if($drinks=='fanta')echo "selected='selected'"; ?>>A Fanta</option>
                                <option value="water"<?php if($drinks=='water')echo "selected='selected'"; ?>>A Water</option>

                        </select>
                    </div>

                    <button type="submit" name="send" value="send" class="btn btn-primary">Send</button>
                    </form>
            </div>
            <!-- -->
        </div>
   
        <div class="row">
            <div class="col-12">
                <div class="alert alert-primary" role="alert">
                    <?php
                        $buttonSend=$_GET['send']??'';

                        if($buttonSend=='send'){
                            if($fruits=='apple'){
                                $total=+$apple;
                            }
                            if($fruits=='banana'){
                                $total=+$banana;
                            }
                            if($fruits=='pineapple'){
                                $total=+$pineapple;
                            }
                            if($fruits=='watermelon'){
                                $total=+$watermelon;
                            }
                            if($fruits=='grapes'){
                                $total=+$grapes;
                            }
                        }

                        if($buttonSend=='send'){
                            if($drinks=='cocacola'){
                                $total=+$cocacola;
                            }
                            if($drinks=='mountaindew'){
                                $total=+$mountaindew;
                            }
                            if($drinks=='pepsi'){
                                $total=+$pepsi;
                            }
                            if($drinks=='fanta'){
                                $total=+$fanta;
                            }
                            if($drinks=='water'){
                                $total=+$water;
                            }
                        }
                    ?>

                </div>
            </div>
        </div>
            <!-- -->        
        <?php
            echo "The total expensive of your lunch is $" . $total . ".00";
        ?>

    </div>
</body>
</html>

php

form-submit

selected

submit-button

0 Answers

Your Answer

Accepted video resources