how to display value from database to tokenize2 select field?

260 views Asked by At

I am using tokenize2 for a user to insert multiple options tags in the post question form. inserting the data into the database works successfully. but similarly, I have an edit page where the inserted form can be edited again. but I am facing an issue in displaying the tags into the field. I am using the below code

at the time of inserting

enter image description here

<?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;

    ?>

above code output enter image description here

for example
$row_question['question_tags'])="india,england,america";
$a[]='india','england','america';

but only India is added to the field and others are not added by the above code.

the problem is what only the first value gets inserted into the field and other values are not inserted. but when I inspect it than I found that the above code run n times with proper values, since I have n values in $a an array. but the only first value is added and the rest of the n values are not added. I don't understand where the mistake is I am doing. correct me if I am doing some mistake somewhere.

1

There are 1 answers

0
Nikhil Vishwakarma On

to solve this problem we have to use the concept of default values. the default value will get some value when the page is loaded. so likewise we have displayed some values fetch from the database when the page is loaded. so just below code solve the problem

<?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;

?>

keep in mind one this selected keyword is important in option tag for displaying predefined values.