Jump to: navigation, search

PHP Array to generate HTML form

From w3cyberlearnings

Contents

Array and for loop to generate the html select element

<?php
        $my_array = array("my name", "my age", "my grade");

        echo '<select name="somethig">';
        echo '<option value="">Select...</option>';
        for ($i = 0; $i < count($my_array); $i++) {
            echo '<option value="' . $i . '">' . $my_array[$i] . '</option>';
        }
        echo '</select>';
?>

Display Result

<select name="somethig">
   <option value="">Select...</option>
   <option value="0">my name</option>
   <option value="1">my age</option>
   <option value="2">my grade</option>
</select> 

generate html select element from associative array

<?php
        $my_array = array("name" => "Johny", "age" => 31, "location" => "Thailand");

        echo '<select name="somethig">';
        echo '<option value="">Select...</option>';
        foreach ($my_array as $k => $v) {
            echo '<option value="' . $k . '">' . $v . '</option>';
        }
        echo '</select>';
?>

Display Result

<select name="somethig">
<option value="">Select...</option>
<option value="name">Johny</option>
<option value="age">31</option>
<option value="location">Thailand</option>
</select> 

php function for html select element

Create the PHP function to generate the HTML SELECT ELEMENT.

       <?php

        function my_drop_down($array, $select_value, $select_name="MY_SELECT_NAME") {

            echo '<select name="' . $select_name . '">';
            echo '<option value="">Select...</option>';
            for ($i = 0; $i < count($array); $i++) {
                if ($array[$i] == $select_value) {
                    echo '<option value="' . $i . '" selected="selected">' . $array[$i] . '</option>';
                } else {
                    echo '<option value="' . $i . '">' . $array[$i] . '</option>';
                }
            }
            echo '</select>';
        }

        // how to use it
        $my_array = array('BMW', 'Ford', 'Honda', 'Toyota', 'Lexus', 'KIA', 'SCION');
        $default_value = 'KIA';

        my_drop_down($my_array, $default_value, 'MY_CAR');
        ?>

Display Result

<select name="MY_CAR">
<option value="">Select...</option>
<option value="0">BMW</option>
<option value="1">Ford</option>
<option value="2">Honda</option>
<option value="3">Toyota</option>
<option value="4">Lexus</option>
<option value="5" selected="selected">KIA</option>
<option value="6">SCION</option>
</select>

generate radio button for html form

       <?php

        function my_radio_btn($array, $radio_name="MY_RADIO_NAME") {
            for ($i = 0; $i < count($array); $i++) {
                echo '<input type="radio" name="' . $radio_name . '" value="' . $array[$i] . '"/>' . $array[$i];
            }
        }

        // how to use it
        $my_array = array('BMW', 'Ford', 'Honda', 'Toyota', 'Lexus', 'KIA', 'SCION');
        my_radio_btn($my_array, 'My_CAR');
        ?>

Display Result

       <input type="radio" name="My_CAR" value="BMW"/>BMW
       <input type="radio" name="My_CAR" value="Ford"/>Ford
       <input type="radio" name="My_CAR" value="Honda"/>Honda
       <input type="radio" name="My_CAR" value="Toyota"/>Toyota
       <input type="radio" name="My_CAR" value="Lexus"/>Lexus
       <input type="radio" name="My_CAR" value="KIA"/>KIA
       <input type="radio" name="My_CAR" value="SCION"/>SCION
Navigation
Web
SQL
MISC
References