如何在jQuery自动完成中将多个值保存到MySQLi DB?

时间:2012-12-28 01:24:54

标签: php jquery jquery-ui mysqli

我刚刚学习了jQuery AutoComplete Widget。这些值很好地弹出逗号。因此,对用户的视觉显示非常棒。

我的问题是如何将多个值保存到MySQLi数据库?

以下是代码:

     <script>
     $(function() {
      var availableskills = [
        "ActionScript",
        "AppleScript",
         ..........
         more languages
        "XML"
    ];
    function split( val ) {
        return val.split( /,\s*/ );
    }
    function extractLast( term ) {
        return split( term ).pop();
    }

    $( "#skills" )
        // don't navigate away from the field on tab when selecting an item
        .bind( "keydown", function( event ) {
            if ( event.keyCode === $.ui.keyCode.TAB &&
                    $( this ).data( "autocomplete" ).menu.active ) {
                event.preventDefault();
            }
        })
        .autocomplete({
            minLength: 0,
            // could have source as a "url"
            source: function( request, response ) {
                // delegate back to autocomplete, but extract the last term
                response( $.ui.autocomplete.filter(
                    availableskills, extractLast( request.term ) ) );
            },
            focus: function() {
                // prevent value inserted on focus
                return false;
            },
            select: function( event, ui ) {
                var terms = split( this.value );
                // remove the current input
                terms.pop();
                // add the selected item
                terms.push( ui.item.value );
                // add placeholder to get the comma-and-space at the end
                terms.push( "" );
                this.value = terms.join( ", " );
                return false;
            }
        });
});
</script>

现在,这是我的输入ID,让用户选择技能:

 <div class="ui-widget">
<!--<label for="skills">Tag programming languages: </label>-->
<input type="text" id="skills" size="50" />
 </div>

不确定如何将用户选择的技能保存到数据库中。我可以轻松地将数据保存到MySQLi数据库;问题是我怎样才能说出三种技能并将它们保存到数据库表中的三个不同的行中。

任何帮助,非常感谢。

1 个答案:

答案 0 :(得分:1)

例如,您将使用输入名称技能

进行表单发布
<form method="post" action="test.php">
<div class="ui-widget">
<input type="text" id="skills" size="50" name='skills' />
</div>
</form>

然后test.php就像

<?php
//...do some mysql connections
$skills = explode(",",$_POST['skills']);
foreach($skills as $skill){
    $statement.="INSERT INTO blahblahblah SET skill = ".mysqli_real_escape_string($skill);
}
$mysqli->multi_query($statement);