dsdzvprlp51692469 2014-05-15 06:09
浏览 74
已采纳

未定义的索引错误4

in my plugin option page, i used an option checkbox. when it checked, this is ok. but when i unchecked it, there showing an error, this :

Undefined index: enable in C:\xampp\htdocs\horror\wp-content\plugins\self-photo-gallery\photo-gallery.php on line 173
/>

the line 173 is :

<input id="spg_settings[enable]" name="spg_settings[enable]" type="checkbox"  value="1" <?php checked(1, $spg_options['enable']); ?> />

but there is another fields

<input type="text" id="spg_settings[twitter_url]" name="spg_settings[twitter_url]" value="<?php echo $spg_options['twitter_url']; ?>"/>

it works fine and didn't showing any error like this.

  • 写回答

3条回答 默认 最新

  • douyigua5381 2014-05-15 06:11
    关注

    Add single quotes.

    Use spg_settings['enable'] instead

    Always use quotes around a string literal array index. For example, $foo['bar'] is correct, while $foo[bar] is not. But why? It is common to encounter this kind of syntax in old scripts:

    <?php
    $foo[bar] = 'enemy';
    echo $foo[bar];
    // etc
    ?>
    

    This is wrong, but it works. The reason is that this code has an undefined constant (bar) rather than a string ('bar' - notice the quotes). PHP may in the future define constants which, unfortunately for such code, have the same name. It works because PHP automatically converts a bare string (an unquoted string which does not correspond to any known symbol) into a string which contains the bare string. For instance, if there is no defined constant named bar, then PHP will substitute in the string 'bar' and use that.

    UPDATE1:

    replace

    <input type="text" id="spg_settings['twitter_url']" name="spg_settings['twitter_url']" value="<?php echo $spg_options['twitter_url']; ?>"/>
    

    with follow line of code

    <input type="text" id="spg_settings['twitter_url']" name="spg_settings['twitter_url']" value="<?php echo (isset($spg_options['twitter_url'])) ? $spg_options['twitter_url'] : ''; ?>"/>
    

    UPDATE2:

    And replace

    <input id="spg_settings['enable']" name="spg_settings['enable']" type="checkbox"  value="1" <?php checked(1, $spg_options['enable']); ?> />
    

    with

    <input id="spg_settings['enable']" name="spg_settings['enable']" type="checkbox"  value="1" <?php checked(1, (isset($spg_options['enable'])) ? $spg_options['enable'] : 0); ?> />
    

    For more information check here

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料