我使用来自 http://loopj.com/jquery-tokeninput/的jQuery Tokeninput自动完成插件当我尝试将值提交到我的 php 处理程序时,我不知道该怎么做
.HTML
<form action="<?=base_lang_url("addtags/test")?>" method="post">
<input id="tags" name="tags">
<input type="submit" value="Add">
</form>
.JS
$('#tags').tokenInput("../ajax/getTags", {
allowCustomEntry: true,
propertyToSearch: "name",
tokenValue: "name",
tokenLimit: 5,
preventDuplicates: true,
theme:"facebook",
noResultsText: "No results",
searchingText: "Searching..."
});
JS只是为了展示我是如何设置它的,但我认为它并不那么重要。
.PHP
if(trim($this->input->post("room_tags")) != ""){
$tags = explode(",",trim($this->input->post("room_tags")));
foreach($tags as $value){
if(preg_replace( "/[^0-9]/", "",$value)){
$tag_int[] = $value;
}else{
$tag_string[] = $value;
}
}
$this->load->model("tags_model");
$tag_find = $this->tags_model->findTags($tag_int);
if(is_array($tag_find) && is_array($tag_string)){
$all_tags = array_merge($tag_find,$tag_string);
}elseif(is_array($tag_find) && isset($tag_find)){
$all_tags = $tag_find;
}elseif(isset($tag_string) && is_array($tag_string)){
$all_tags = $tag_string;
}
echo "<pre>";
echo print_r($all_tags);
echo "</pre>";
}
结果
Array
(
[0] => Array
(
[tag_name] => Hej
)
[1] => Array
(
[tag_name] => Hvor
)
[2] => Array
(
[tag_name] => Hvad
)
[3] => 'wegehe'
[4] => 'afqfq'
)
我喜欢这样:
Array
(
[0] => Hej
[1] => Hvor
[2] => Hvad
[3] => wegehe
[4] => afqfq
)
字符串上没有 '。
希望有人能帮助:)
在前面添加此代码
echo '<pre>';
$all_tags_copy = $all_tags;
$all_tags = array();
foreach($all_tags as $key => $value){
if(is_array($value) && count($value) > 0){
foreach($value as $v2){
if(!empty($v2)){
$all_tags[] = $v2;
}
}
}else{
if(!empty($value)){
$all_tags[] = $value;
}
}
}