• Resolved mugluck

    (@mugluck)


    I’m fairly new to wordpress, so it’s really hard for me to track where the variables are coming from and how information is being generated.

    I’m currently trying to debug my companies wordpress site and they have some custom code in their search dropdowns that’s generating a bunch of errors

    Notice: Undefined index: searchlist in C:\\xampp\\htdocs\\mysite\\wp-content\\themes\\mytheme\\sidebar-list.php on line 102

    The offending code in question:
    line 102:
    <input type="text" name="searchlist" value="<?php echo $_GET['searchlist']; ?>" id="searchBar" placeholder="Keyword" />

    The full code:

    <?php dynamic_sidebar('listwid');?>
     
     
     <form  action="<?php bloginfo("url") ?>/search-listing" method="get">
     <select name="listsearchloaction" class="listdrop">
     <option value="">Choose Location</option>
     <?php   
       $args = array(
        'orderby'           => 'name', 
        'order'             => 'ASC',
        'hide_empty'        => 0, 
        'exclude'           => array(), 
        'exclude_tree'      => array(), 
        'include'           => array(),
        'number'            => '', 
        'fields'            => 'all', 
        'slug'              => '', 
        'parent'            => '',
        'hierarchical'      => true, 
        'child_of'          => 0, 
        'get'               => '', 
        'name__like'        => '',
        'description__like' => '',
        'pad_counts'        => false, 
        'offset'            => '', 
        'search'            => '', 
        'cache_domain'      => 'core'
    ); 
    $list_terms = get_terms( 'listing_category', $args );
    foreach ( $list_terms as $list_term)
    { ?>
     
     <option value="<?php echo $list_term->term_id;?>" <?php if($_GET['listsearchloaction'] == $list_term->term_id) { echo 'selected="selected"';}?>><?php echo $list_term->name;?></option>
     <?php } ?>
     </select>
    
     <option value="<?php echo $list_term->term_id;?>" <?php if($_GET['listsearchspecial'] == $list_term->term_id) { echo 'selected="selected"';}?>><?php echo $list_term->name;?></option>
     <?php } ?>
     </select>
     
     <input type="text" name="searchlist" value="<?php echo $_GET['searchlist']; ?>" id="searchBar" placeholder="Keyword" />
     <input type="submit" value="GO" id="searchBtn"/>
     </form> 

    As I understand it, it’s trying to pull information to populate a dropdown list, but because the array hasn’t been loaded until the form is posted, it spits out that error. (I think) But as I’m not a wordpress developer, I can’t be certain, nor can I figure out how it’s loading the information at all.

    Anybody have any idea how to enlighten me on this matter? What should I be looking for?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    Yep, the index searchlist does not exist in $_GET['searchlist'];

    Try using isset to check if the index exists:
    http://php.net/manual/en/function.isset.php

    Example

    
    <?php dynamic_sidebar('listwid');?>
     
     
     <form  action="<?php bloginfo("url") ?>/search-listing" method="get">
     <select name="listsearchloaction" class="listdrop">
     <option value="">Choose Location</option>
     <?php   
       $args = array(
        'orderby'           => 'name', 
        'order'             => 'ASC',
        'hide_empty'        => 0, 
        'exclude'           => array(), 
        'exclude_tree'      => array(), 
        'include'           => array(),
        'number'            => '', 
        'fields'            => 'all', 
        'slug'              => '', 
        'parent'            => '',
        'hierarchical'      => true, 
        'child_of'          => 0, 
        'get'               => '', 
        'name__like'        => '',
        'description__like' => '',
        'pad_counts'        => false, 
        'offset'            => '', 
        'search'            => '', 
        'cache_domain'      => 'core'
    );
     
    $list_terms = get_terms( 'listing_category', $args );
    $location   = isset( $_GET['listsearchloaction'] ) ? $_GET['listsearchloaction'] : '';
    $special    = isset( $_GET['listsearchspecial']) ? $_GET['listsearchspecial'] : '';
    $searchlist = isset( $_GET['searchlist']) ? sanitize_text_field( $_GET['searchlist'] ) : '';
    
    foreach ( $list_terms as $list_term)
    { ?>
     
     <option value="<?php echo $list_term->term_id;?>" <?php if($location == $list_term->term_id) { echo 'selected="selected"';}?>><?php echo $list_term->name;?></option>
     <?php } ?>
     </select>
    
     <option value="<?php echo $list_term->term_id;?>" <?php if($special == $list_term->term_id) { echo 'selected="selected"';}?>><?php echo $list_term->name;?></option>
     <?php } ?>
     </select>
     
     <input type="text" name="searchlist" value="<?php echo $searchlist; ?>" id="searchBar" placeholder="Keyword" />
     <input type="submit" value="GO" id="searchBtn"/>
     </form>
    
    Thread Starter mugluck

    (@mugluck)

    Thanks! That helps a lot actually. It solves that problem for sure, but now I’ve discovered this is the case with all of this particular area of our wordpress site. Oh boy, this is going to be fun…

    Anyway, thanks for taking the time to answer my noob question.

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Unpacking an Undefined Index error’ is closed to new replies.