• First of all, I tried to post in WP-Advanced but see that only moderators can post there, so I have added a modlook tag.

    I am trying to create an autosuggest as in this post (https://www.htmlblog.us/jquery-autocomplete) which searches one of my own tables. I am using the Allow PHP in Posts and Pages plugin.

    I can get the form to work

    echo "<html>";
    echo "<head>";
    echo "<script type='text/javascript' src='jquery/js/jquery-1.4.2.min.js'></script>";
    echo "<script type='text/javascript' src='jquery/js/jquery-ui-1.8.2.custom.min.js'></script>";
    echo "<script type='text/javascript'> jQuery(document).ready(function(){
    $('#zipsearch').autocomplete({source:'/thegrapevine/?page_id=219', minLength:2});
    });
    
    </script>";
    echo "<link rel='stylesheet' href='jquery/css/smoothness/jquery-ui-1.8.2.custom.css' />";
    echo "<style type='text/css'><!--
    
    /* style the auto-complete response */
    li.ui-menu-item { font-size:12px !important; }
    
    --></style>";
    echo "</head>";
    
    echo "<body>";
    
    echo "<form onsubmit='return false;'>Enter a Zipcode:<input id='zipsearch' type='text' />";
    echo "</form>";
    
    echo "</body>";
    echo "</html>";

    but the call to the php script that searches the table is not working.

    $rs = mysql_query('select zip, city, state from zipcode where zip like "'. mysql_real_escape_string($_REQUEST['term']) .'%" order by zip asc limit 0,10', $dblink);
    
    $data = array();
    if ( $rs && mysql_num_rows($rs) )
    {
        while( $row = mysql_fetch_array($rs, MYSQL_ASSOC) )
        {
            $data[] = array(
                'label' => $row['zip'] .', '. $row['city'] .' '. $row['state'] ,
                'value' => $row['zip']
            );
        }
    }
    
    echo json_encode($data);
    flush();

    I have tried placing the code in a page and as a template but neither work.

    As anyone done anything like this?

    Thanks

Viewing 2 replies - 1 through 2 (of 2 total)
  • Try looking at plugins that do auto suggest for the search box

    Moderator bcworkz

    (@bcworkz)

    I’ve not implemented autocomplete in WP, but in general it is a basic AJAX request where the server’s response populates a selection box of some sort. Nothing fancy really. The real problem is AJAX in WP is not like in other applications, it has particular quirks that must be addressed in order to have reliable results. A generic example not specific to WP will likely fail.

    Thus phillbooth’s suggestion is a good one, find a WP specific example from which to work from.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Creating custom autosuggest query’ is closed to new replies.