我正在使用jQuery Tag-it!为我的用户创建一个"技能"输入表单。我有标签它的 UI 工作,但我无法将用户输入到 PHP 数组中。我正在尝试序列化此数组并将其保存到 mysql 数据库中以供以后显示,但我什至无法将数据放入数组中。
这是初始化tag-it的javascript:
$('#skills').tagit({
allowSpaces: true,
placeholderText: "Separate skills with a comma please",
autocomplete: true
});
这是 HTML:
<div>
<label class="add_label">Skills: </label>
<ul id="skills" style="width: 275px; margin-bottom: 8px;"></ul>
</div>
这是创建应该存储用户输入的输入字段的javascript:
if (!this.options.singleField) {
var escapedValue = label.html();
tag.append('<input type="hidden" style="display:none;" value="' + escapedValue + '" name="' + this.options.fieldName + '" />');
}
这是获取用户输入的PHP--这是不工作的部分。我无法从表单中检索任何数据:
$skillsArr = $link->real_escape_string($_POST['skills']);
当我提交表单时,mysqli 查询将执行,并且在数据库中我看到序列化数组应该在哪里的"N;"。
如何将jQuery Tag-it值获取到PHP数组中,我可以序列化并保存到mysql数据库中?
问题是tag-it默认情况下会发送带有如下数据的post请求:
tags=foo&tags=bar&tags=blah
PHP 将通过使 $_POST['tag'] ='blah' 来解释这一点。 为了使 PHP 像数组一样处理它,发布数据需要如下所示:
tags[]=foo&tags[]=bar&tags[]=blah
解决此问题的最简单方法是在设置 tag-it 时更改 fieldName 参数,例如:
$('.taglist').tagit({
allowSpaces: true,
placeholderText: 'add new tag here...',
fieldName: 'tags[]'
});
通过简单地更改名称以包含 [],那么它将由 PHP 根据需要插入并成为一个数组。
或者,如果您无法调整,您可以随时处理原始 PHP 数据以将标签作为数组获取,例如:
$query = array();
foreach (explode('&', file_get_contents('php://input')) as $kv) {
list($k, $v) = explode('=', $kv);
$k = urldecode($k);
$v = urldecode($v);
if (!isset($query[$k])) {
$query[$k] = $v;
} else {
if (is_array($query[$k])) {
$query[$k][] = $v;
} else {
$query[$k] = array($query[$k], $v);
}
}
}
现在$query['标签']将按预期成为一个数组。
注意:如果只发送一个标签,那么它最终会成为带有上述代码的字符串,所以如果结果在循环或其他东西中,请确保将其转换为数组:
foreach((array)$query['tags'] as $tag) ...
我发现在后端(php/mysqli)上做所有查询更容易。
这样,我在jQuery自动完成中唯一需要的是:
<script>
$(document).ready(function(){
$("#tagit").tagit({
autocomplete: {
source: "ajax-search.php",
}
});
});
</script>
我刚刚定义了文件的来源。您可以根据需要添加分隔符等(我只是修改了源代码)。
但主要函数来自 php 文件,该文件返回 JSON 编码的结果。
<?php
include("dbconnect.php"); //Including our DB Connection file
if ( !isset($_REQUEST['term'])) //if there's no search, exit
exit;
$keyword = trim($_REQUEST['term']);
$keyword = mysqli_real_escape_string($db, $keyword);
$query = "SELECT * FROM animals WHERE english LIKE '%$keyword%' LIMIT 10";
$result = mysqli_query($db, $query); //Run the Query
$data = array(); //initialize array
if ($result && mysqli_num_rows($result)){
while($row = mysqli_fetch_assoc($result)){
$data[] = array(
'id' => $row['row_id'],
'label' => $row['english'], //This is the 'live return of the search
'value' => $row['eng_dir'], //The value returned. Not needed if you're returning the label
'latin' => $row['latin'] //Another result (you can add more)
);
}
}
echo json_encode($data);
flush();
?>
然后在 tag-it.js 文件中,您可以选择要推送的内容作为标签:
if (this.options.availableTags || this.options.tagSource || this.options.autocomplete.source) {
var autocompleteOptions = {
select: function(event, ui) {
that.createTag(ui.item.id); //pushes the ID
that.createTag(ui.item.value); //pushes the value
that.createTag(ui.item.label); //pushes the label
that.createTag(ui.item.latin); //pushes the extra variable
// Preventing the tag input to be updated with the chosen value.
return false;
}
};
$.extend(autocompleteOptions, this.options.autocomplete);
上面的代码将返回同一标签的 4 个实例,具体取决于您的结果。