我正在使用tokenize2为用户在帖子问题表单中插入多个选项标签。成功地将数据插入数据库。但是类似地,我有一个编辑页面,其中插入的表单可以再次编辑。但是在字段中显示标签时,我遇到了一个问题。我正在使用下面的代码
在插入时
<?php
$a=array();
$a=explode(',',$row_question['question_tags']);
$output="";
foreach($a as $v)
{
$output.="<script>$('.tokenize-callable-demo1').tokenize2().trigger('tokenize:tokens:add', ['token value','".$v."' , true]); </script>";
}
echo $output;
?>以上代码输出enter image description here
for example
$row_question['question_tags'])="india,england,america";
$a[]='india','england','america';但上面的代码只将印度添加到字段中,而不添加其他字段。
问题是,只有第一个值被插入到字段中,而其他值没有被插入。但是当我检查它时,我发现上面的代码使用正确的值运行了n次,因为我在$a一个数组中有n个值。但是只添加了第一个值,并且不添加n个值中的其余值。我不明白我做错了什么。如果我在什么地方做错了,请纠正我。
发布于 2020-12-18 18:18:09
为了解决这个问题,我们必须使用默认值的概念。当页面加载时,默认值将获得一些值。因此,当页面加载时,我们同样显示了从数据库获取的一些值。所以下面的代码就解决了这个问题
<?php
$a=array();
//in database i store values with "," thats why i am using explode to break them and treat as single option
$a=explode(',',$row_question['question_tags']);
$output="";
foreach($a as $v)
{
$output.="<option value=".$v." selected>".$v."</option>";
}
echo $output;
?>请记住,在用于显示预定值的选项标记中,此selected关键字非常重要。
https://stackoverflow.com/questions/65344349
复制相似问题