MyBB Hacks

Full Version: Adding auto-completion
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello there,

I want to use XThread with an auto-completed field (multiline one) allowing to choose a previous entry (comming from a separate table) or to add a new one.

Does anybody already did that ?
If no, I'll try to do and give you my solution Smile
So, you didn't help me but I'm a really nice guy and give you my solution Smile

Input Field Type : Multiline textbox
Allow multiple values for this field : yes
Use Custom Input HTML : yes
Input Field HTML :

Code:
<textarea{NAME_PROP}{MAXLEN_PROP}{HEIGHT_PROP_ROWS}{WIDTH_PROP_COLS}{TABINDEX_PROP}{REQUIRED_PROP} id="xthreads_{KEY}">{VALUE}</textarea>
<script type="text/javascript">new autoComplete("xthreads_{KEY}", "ajax_authors.php?action=get", {valueSpan: "authname", delimChar: "\n"});</script>

I used my own ajax_authors.php (see below) because it's an external DB (not in mybb_structure) and I didn't want to modify xmlhttp.php

Display Parsing : Plain text with new lines
Multiple Value Delimiter : &

ebook_newthread :

Code:
<html>
<head>
<title>{$lang->newthread_in}</title>
{$headerinclude}
<script type="text/javascript" src="jscripts/post.js?ver=1400"></script>
<script type="text/javascript" src="jscripts/autocomplete.js?ver=1400"></script>
</head>
<body>
{$header}
{$preview}
{$thread_errors}
{$attacherror}
<form action="newthread.php?fid={$fid}&amp;processed=1" method="post" enctype="multipart/form-data" name="input">
<input type="hidden" name="my_post_key" value="{$mybb->post_code}" />
<table border="0" cellspacing="{$theme['borderwidth']}" cellpadding="{$theme['tablespace']}" class="tborder">
<tr>
<td class="thead" colspan="2"><strong>{$lang->post_new_thread}</strong></td>
</tr>
{$loginbox}
<tr>
<td class="trow2" width="20%"><strong>Ebook title</strong></td>
<td class="trow2">{$prefixselect}<input type="text" class="textbox" name="subject" size="40" maxlength="85" value="{$subject}" tabindex="1" /></td>
</tr>
{$extra_threadfields}{$posticons}
<tr>
<td class="trow2" valign="top"><strong>{$lang->your_message}</strong>{$smilieinserter}</td>
<td class="trow2">
<textarea name="message" id="message" rows="20" cols="70" tabindex="2">{$message}</textarea>
{$codebuttons}
{$multiquote_external}
</td>
</tr>
<tr>
<td class="trow1" valign="top"><strong>{$lang->post_options}</strong></td>
<td class="trow1"><span class="smalltext">
{$option_signature}
{$disablesmilies}</span></td>
</tr>
{$modoptions}
{$subscriptionmethod}
{$pollbox}
{$captcha}
</table>
{$attachbox}
<br />
<div style="text-align:center"><input type="submit" class="button" name="submit" value="{$lang->post_thread}" tabindex="4" accesskey="s" />  <input type="submit" class="button" name="previewpost" value="{$lang->preview_post}" tabindex="5" />{$savedraftbutton}</div>
<input type="hidden" name="action" value="do_newthread" />
<input type="hidden" name="posthash" value="{$posthash}" />
<input type="hidden" name="attachmentaid" value="" />
<input type="hidden" name="attachmentact" value="" />
<input type="hidden" name="quoted_ids" value="{$quoted_ids}" />
<input type="hidden" name="tid" value="{$tid}" />
{$editdraftpid}
</form>
{$forumrules}
{$footer}
</body>
</html>


ajax_authors.php :
(Notice that I've a small trouble in render, the lines after the first one begins with a space)

Code:
<?php
try {
   // It's an example, you can use mybb conf and db object
   $db['host'] = 'localhost';
   $db['base'] = 'mybase';
   $db['user'] = 'myuser';
   $db['pass'] = 'mypass';
   $pdo = new PDO("mysql:host=".$db['host'].";dbname=".$db['base'], $db['user'], $db['pass']);
} catch (PDOException $e) {
   echo "Failed to get DB handle: " . $e->getMessage() . "\n";
   exit;
}

// Send no cache headers
header("Expires: Sat, 1 Jan 2000 01:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");
header("Content-type: text/plain; charset=utf8");

if (isset($_GET['query']) && strlen($_GET['query'])>3) {
   $q = str_replace(array(' ', '&'), array('', ''), $_GET['query']);
   $query = $pdo->prepare("SELECT aid, aname FROM ref_authors WHERE aname LIKE :aname AND valid=1");
   $query->execute(array(':aname' => '%'.$q.'%'));
   $authList = array();
   while ($row = $query->fetch(PDO::FETCH_ASSOC)) {
        echo '<div>', PHP_EOL, '<span class="authname">', utf8_encode($row['aname']), '</span>', PHP_EOL, '</div>', PHP_EOL;
   }
}


I'll try to correct some small bugs, but it works Smile

Thanks for sharing.
Pulling values from a custom table like this is somewhat custom behaviour and will probably require different code depending on what the requirements are.
Here it just looks like you just want an auto-complete as opposed to enforcing values that already exist in the table.
Well, my actual trouble is that I've a reference table, but I don't want to limit the users to its value, and I don't want to let the users adding their own values in the reference (I've a system to update references).
So, I use autocompletion as an helper for user entries, and xthread for display and search.

I'll work soon on the way to have the auto-completion fully working with xthread for systems as tags, with the possibility to automaticaly add new entries in the field definition.
Reference URL's