How to Insert an Item Combobox in PHP

Save

Combo boxes or drop-down menus are a staple in online forms. Combo boxes often show a list of common choices, such as a list of countries or states, to make it easier and faster for users to fill out a form. Using the Web programming language PHP, you can create a combo box with items generated by PHP itself. The use of PHP arrays, let you define items to be placed in a combo box. The same method can also be used to populate a combo box with items from a database.

  • Open a new file in Notepad.

  • Type in the PHP code to place combo box items in an array:

    <?php

    $items = array ('apple','banana','orange');

    ?>

  • Add the HTML code for the combo box:

    <select name="select">

    <option value="Choose a fruit" selected>Choose a fruit</option>

  • Insert the array items within the combo box using a PHP loop:

    <?php

    for each ($items as $value) {

    ?>

    <option value="<?php echo $value; ?>"><?php echo $value; ?></option>

    <?php

    }

    ?>

    </select>

  • Save it as a PHP file.

References

  • Photo Credit Comstock/Comstock/Getty Images
Promoted By Zergnet

Comments

Related Searches

Check It Out

Geek Vs Geek: Robot battles, hoverboard drag race, and more

M
Is DIY in your DNA? Become part of our maker community.
Submit Your Work!