将jQuery标记保存为php数组输入

将jQuery标记保存为php数组输入,php,jquery,arrays,jquery-ui,tag-it,Php,Jquery,Arrays,Jquery Ui,Tag It,我正在使用jQuery标记它!为我的用户创建“技能”输入表单。我有标记它的UI,但我无法将用户输入到PHP数组中。我试图序列化这个数组并将其保存到mysql数据库中,以便以后显示,但我甚至无法将数据放入数组中 以下是javascript初始化标记: $('#skills').tagit({ allowSpaces: true, placeholderText: "Separate skills with a comma please", autocomplete

我正在使用jQuery标记它!为我的用户创建“技能”输入表单。我有标记它的UI,但我无法将用户输入到PHP数组中。我试图序列化这个数组并将其保存到mysql数据库中,以便以后显示,但我甚至无法将数据放入数组中

以下是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>
当我提交表单时,会执行mysqli查询,在数据库中我看到“N;”,序列化数组应该在哪里


如何将jQuery标记it值放入PHP数组中,然后将其序列化并保存到mysql数据库?

问题是,标记it在默认情况下会使用如下数据发送post请求:

tags=foo&tags=bar&tags=blah
PHP将通过使$_POST['tag']='blah'来解释这一点。为了让PHP像处理数组一样处理它,post数据需要如下所示:

tags[]=foo&tags[]=bar&tags[]=blah
解决此问题的最简单方法是在设置标记时更改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['tags']将如预期的那样成为一个数组

注意:如果只发送了一个标记,那么它最终将是一个包含上述代码的字符串,因此,如果结果是循环或其他形式,请确保将其转换为数组:

foreach((array)$query['tags'] as $tag) ...

我发现在后端(php/mysqli)执行所有查询更容易

这样,我在jQuery自动完成中唯一需要的就是:

<script>
$(document).ready(function(){
  $("#tagit").tagit({
    autocomplete: {
   source: "ajax-search.php",
}
  });
});
</script>

根据您的结果,上面的代码将返回同一标记的4个实例。

在某个地方回显mysql查询,或者让它转到错误日志(),以便您可以查看实际运行的查询。尝试
var\u转储($\u请求)
在php处理程序文件中查看您提交的数据并进行相应处理。或者在控制台日志中查看随ajax请求发送的帖子。您能向我们解释一下您对这个问题的解决方案吗@TyBailey
foreach((array)$query['tags'] as $tag) ...
<script>
$(document).ready(function(){
  $("#tagit").tagit({
    autocomplete: {
   source: "ajax-search.php",
}
  });
});
</script>
<?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();
  ?>
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);