注意:未定义索引:PHP中的关联数组,带有帮助程序类

时间:2018-08-01 09:49:21

标签: php html helpers

我有一个具有以下代码的helper_class.php:

<?php
// print a <select> menu
function input_select($element_name, $selected, $options, $multiple = false) {
     // print out the <select> tag
     print '<select name="' . $element_name;
     // if multiple choices are permitted, add the multiple attribute
     // and add a [] to the end of the tag name
     if($multiple) {
         print '[]" multiple="multiple';
     }
     print '">';
     // set up the list of things to be selected
     $selected_options = array();
     if($multiple) {
         foreach($selected[$element_name] as $val) {
             $selected_options[$val] = true;
         }
     } else {
         $selected_options[$selected[$element_name]] = true;
     }
     // print out the <option> tags
     foreach($options as $option => $label) {
         print '<option value="' . htmlentities($option) . '" ';
         if($selected_options[$option]) {
             print 'selected="selected"';
         }
         print '>' . htmlentities($label) . '</option>';
     }
     print '</select>';
}
?>

我有一个complete_form.php文件,其中使用以下代码使用input_select()方法创建了一个选择输入:

<?php

require_once('helper_class.php');
$colors = array('pink' => 'Absolute Pink',
                'red' => 'Infinite Red');
function show_form() {
    if($_POST['_submit_check']) {
        $defaults = $_POST;
    } else {
        // Otherwise set default color red
        $defaults = array('red' => 'Infinite Red');
    }
?>
    <form method="post" action="<?php print $_SERVER['PHP_SELF']; ?>">
    <table>
    <tr><td> Pick one color: </td>
    <td><?php input_select('color', $defaults, $GLOBALS['colors']); ?>
    </td></tr>
    </table>
    <input type="hidden" name="_submit_check" value="1"/>
    </form>
<?php
} //end of show_form()
show_form();
?>

但是在select而不是颜色名称中,我得到:

  

注意:未定义索引:第24行> C:\ xampp \ htdocs ...中的粉红色>绝对粉红色。

请帮助我找到解决此问题的方法。

Edit1:由于引用的问题没有帮助程序类,因此该问题并不是真正的重复项。我不明白为什么通知会出现在选择输入的内部,而不是显示在输入的上方。

Edit2:我找到了解决问题的方法,同样,答案不在所引用的问题中。要提供解决方案,请将该问题取消标记为重复,以便我回答。

0 个答案:

没有答案
相关问题