0

I'm using Jquery Tag it and I would like to save the entered tag into my DB.

In HTML

<ul id="myTags" name="myTags"></ul>  

When I entered a new tag , it will auto generate a <li> and <input hidden> , But all the generated <input hidden> are same name="tags" only the value are different , then when I $_POST and try to print_r , the result are only the last value that I entered.

How do I get all the value ? or any other good suggestion ? Thanks

    public function actionSubmit(){

    $token = $_POST['YII_CSRF_TOKEN'];              
    if ($token !== Yii::app()->getRequest()->getCsrfToken()){

        Yii::app()->end();
    }
    $app_id = Yii::app()->user->getState('app_id');
    $tags = $_POST['tags'];


    $model = Games::model()->findByAttributes(array('id'=>$app_id));
    $model->keywords = $tags;
    $model->save();

    if($model->save()){
        print_r("ok");  
    }else{
        print_r("no");  
    }
}
TheSmile
  • 358
  • 3
  • 9
  • 34

4 Answers4

1

try

$("#myTags").tagit({
    fieldName: "tags[]"
});
itachi
  • 6,323
  • 3
  • 30
  • 40
  • yeah this `print_r` correctly ... but it is array now ... how to save array to db ? because now i getting this message from browser **Array to string conversion** – TheSmile Nov 12 '14 at 06:48
  • you can do it with three ways. _Active Record_ doesn't support batch inserting in yii so you need to loop and insert one by one. On the other hand, both _query builder_ and _DAO_ does support batch insert. which way you want? – itachi Nov 12 '14 at 06:56
  • In db the data is `["qwe","ert"]` – TheSmile Nov 12 '14 at 07:00
  • just a note.... if you are storing them as json, and if you need to search among the entries, then use nosql database engines. otherwise, the commands will be slow in relational databases like mysql. [**read here**](http://stackoverflow.com/questions/15367696/storing-json-in-database-vs-having-a-new-row-for-each-key) for further explanation. – itachi Nov 12 '14 at 07:02
0

Try below code:

//jquery code, on form submit
$('#form-id').submit(function(){

   var allTags = ''
   $('input:[name=tags]').each(function(){
       allTags += "," + $(this).val();
   });

   allTags = allTags.substr(1);

});
user3473830
  • 7,165
  • 5
  • 36
  • 52
0

Try name="tags[]" in your <input hidden> tag.

Basically create array of hidden variable in HTML with same name as name="tags[]"

So, when you get tags from $_POST then it's return tags array from $_POST['tags']

kupendra
  • 1,002
  • 1
  • 15
  • 37
0

just show a demo use this code and run your browser

<?php

if(isset($_REQUEST['submit']))
{
 print_r( $_POST['tags'] );
}
?>
<body>
<form method="post">
    <ul id="myTags" name="myTags" style="list-style:none;">
        <li><input type="hidden" value="1" name="tags[]" /></li>
        <li><input type="hidden" value="2" name="tags[]" /></li>
        <li><input type="hidden" value="3" name="tags[]" /></li>
        <li><input type="hidden" value="4" name="tags[]" /></li>
        <li><input type="hidden" value="5" name="tags[]" /></li>
    </ul> 
    <input type="submit" name="submit" value="submin" />
</form>
</body>

use name is also array

<li><input type="hidden" value="...any value ...." name="tags[]" /></li>
Avinash Antala
  • 641
  • 5
  • 10