我正在尝试将多个选择值插入数据库。
HTML
<select style="width:175px" id="first" multiple="true">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
</div>
<div class="mid">
<button type="button" class='add'> > </button>
<button type="button" class='remove'> < </button>
<button type="button" class='add-all'> >>> </button>
<button type="button" class='remove-all'> <<< </button>
</div>
<div class="end">
<select style="width:175px" id="second" name="second[]" multiple="true">
</select>
</div>
<div style="clear:both;"></div>
使用Javascript:
$('.add').click(function(){
$('#first option:selected').appendTo('#second');
});
$('.remove').click(function(){
$('#second option:selected').appendTo('#first');
});
$('.add-all').click(function(){
$('#first option').appendTo('#second');
});
$('.remove-all').click(function(){
$('#second option').appendTo('#first');
});
PHP :(它只是PHP代码的一小部分)
$formvars['second'] = $this->Sanitize($_POST['second']);
$insert_query = 'insert into 'comments'(second) values ("' . $this->SanitizeForSQL($formvars['second']) . '",)';
它只将“Array”一词插入数据库。有什么想法吗?
答案 0 :(得分:0)
var selectArr = [];
$('#first option').each(function() {
selectArr.push($(this).val());
});
现在您可以将selectArr传递给您的服务器,然后检查您获得的内容。
答案 1 :(得分:0)
如果它正在插入一个数组,你应该通过for循环迭代它来连接一个字符串中的所有值:
<?php
$toInsert = '';
foreach($_POST['second'] as $p){
$toInsert = $toInsert . $p;
}
?>
$ toInsert字符串是您应该写入数据库的字符串。