It is working for me as charm.
I had need custom entry which will store database and auto increment id will be the value of this token.
I have fixed this as following. There is a tricks, on add token a request goes to server add new token to database and its new insert id comes to client side and set as value of added token.
Get plugin with allow free tagging facility from github.
<input type="text" name="w_i_tk" id="w_i_tk">
<script>
$(document).ready(function() {.
$("#w_i_tk").tokenInput("token.php", {
theme: "facebook",
hintText: "Type tag by which other can search, e.g. PHP, MySql etc.",
preventDuplicates: true,
tokenLimit: 5,
minChars: 2,
onAdd: function (item) {
if(item.id=="0") {
$.ajax({
type:"GET",
url:"token.php",
data:{action:"newtoken",name:item.name},
success: function(resp) {
$("#w_i_tk").tokenInput("remove", {name: item.name});
$("#w_i_tk").tokenInput("add", {id: resp, name: item.name});
}
});
}
},
animateDropdown: false,
allowFreeTagging: true
});
});
</script>
token.php
<?php
if(isset($_GET["q"])) {
$q = trim($_GET["q"]);
$isSearchItemExists = false;
$sql = sprintf("SELECT token_id, token from tokens WHERE token LIKE '%%%s%%' ORDER BY popularity DESC LIMIT 10", mysql_real_escape_string($q));
$rec = mysql_query($sql);
$arr = array();
while($row = mysql_fetch_array($rec)) {
$obj = new stdClass();
$obj->id = $row["token_id"];
$obj->name = $row["token"];
if($obj->name==$q) {
$isSearchItemExists = true;
}
$arr[] = $obj;
}
if(!$isSearchItemExists) $arr = array_merge(getNewToken($q),$arr);
$json_response = json_encode($arr);
echo $json_response;
} else if(isset($_GET["action"]) && $_GET["action"]=="newtoken") {
$token = strtolower($_REQUEST["name"]);
$sql = "SELECT * FROM tokens WHERE token='$token'";
$rec = mysql_query($sql);
$numRows = mysql_num_rows($rec);
if($numRows>0) {
$row = mysql_fetch_array($rec);
$id = $row["token_id"];
} else {
$sql = "INSERT INTO tokens SET token='$token'";
$rec = mysql_query($sql);
$id = mysql_insert_id();
}
echo $id;
exit;
}
function getNewToken($q) {
$sql = "SELECT max(token_id) as token_id FROM tokens";
$rec = mysql_query($sql);
$row = mysql_fetch_array($rec);
$maxToken = $row["token_id"];
$newToken = $maxToken + 1;
$newItem = array();
$new = new stdClass();
$new->id = "0";
$new->name = $q;
$newItem[] = $new;
return $newItem;
}
?>