简体   繁体   中英

Get values of inputs with same name

Ok, I have a task to write a simple php ticket system. I can't work out how to get all values from input called ages

My code looks like this and I don't really know where to go from here

<form id="form" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
        <fieldset>
            <label for="number">Ticket count </label>
            <input type="number" name="number" />
            <input id="submit" type="submit" value="Set ages" class="submit"/>

            <?php
                $numberOfTickets = $_POST['number'];
                for($i=1;$i <=$numberOfTickets;$i++){
                    echo "<select class='ages' name='ages'>";
                    echo "<option value='adult'>18 år och uppåt</option>";
                    echo "<option value='youth'>13 – 18 år</option>";
                    echo "<option value='child'>7 – 12 år</option>";
                    echo "<option value='baby'>under 7  år</option>";
                    echo "</select>";
                }
            ?>

        <input class="submit" type="submit" name="submit" value="Beställ biljett" />
        </fieldset>
    </form>
    </div><!--content-->
    <?php
        class Ticket
        {           
          public function price($age)
          {
            return $price;
          }
          public function ages($ages)
          {
                foreach($ages as $f)
                {
                    echo "$f</br>";
                }
          }

        }
    ?>

As the <select> is inside a loop, I assume there are more than one select named "ages". If that is the case, do this:

echo "<select class='ages' name='ages[]'>";

And to get the values in PHP, use this:

$_POST['ages']

That will be an array. Use for, foreach, implode or whatever you need to convert the arrays back to text.

Please explain more if this is not what you meant.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM