Jump to content

typeahead (ajax) search box


Recommended Posts

May you please advise? :idea:


I've got tons of notes and tabs, and I feel like I'm really close. If you would like to lend some of your time and wisdom, I would be most grateful. I'm looking to contribute my first community addon to WHMCS.


I wish to query the tblknowledgebase table and return title and id into typeahead array that'll then open the echo in new tab / window. Did I say that right? :D




This is where I'm at now:


   $array = array();
   $query=mysql_query("select * from tblknowledgebase where title LIKE '%{$key}%'");
   // I just duped above line and replaced title with ID
   $query=mysql_query("select * from tblknowledgebase where id LIKE '%{$key}%'");
     $array[] = [u]$row['title'][/u];
     // missing the row ID stuff and concenate stuff

   echo json_encode($array);





This is what I desire output of $array[]:


<a href="knowledgebase.php?action=displayarticle&id={$kbarticle.id}" target="_blank">$row['title']</a>

<a href="knowledgebase.php?action=displayarticle&id=$row['title']" target="_blank">$row['title']</a>



Href stuff pulled from file: supportticketsubmit-kbsuggestions.tpl

Starting tutorial: https://codeforgeek.com/2014/09/ajax-search-box-php-mysql/


keywords used during research

• typeahead mysql fetch "link" + "href"

• add href OR link to $array[] = $row[

• json_encode href

• php concenate $row['



• concenate

• mysql = deprecated?

• mysqli = new?









Link to comment
Share on other sites

Hey! The API can be a bit confusing to get around at first. It's very exciting making your first add-on.


Please do not use mysql_connect - use mysqli_connect! mysql_connect is deprecated and it has actually been removed all together in PHP 7.0!



Also you should filter variables if you're going to use them in the database. For example:


// this is _NOT_ safe to use in a query
$key = $_GET['key']; // BAD 

// this _IS SAFE_ to use in a query
$key = mysqli_real_escape_string($_GET['key']); // BETTER 


Hers an article on it: http://www.tizag.com/mysqlTutorial/mysql-php-sql-injection.php


OK now, with this:


     $array[] = $row['title'];



You are only storing the title. You will want to grab the ID and title with this:


$array[] = [ 
   "id" => $row["id"],
   "title" => $row["title"]


You would then get an array of objects in JSON like this:


       "id": 1,
       "title": "Your Title",
       "id": 2,
       "title": "Another Title",
       "id": 3,
       "title": "We love WHMCS",

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated