所以,我有一个HTMLPHP表单,它有一个选择列表框,你可以从这个列表框中选择多个值,因为它的多个属性被设置为多个。考虑表单方法是'POST'。但是当我点击SUBMIT时,列表框只保存了一个值(具体到最后选择的值)。我不知道为什么。
这是我代码的一部分。
<?php
if(isset($_POST['submitSave'])) {
// Disable errors due to empty xml files
error_reporting(E_ALL & ~E_WARNING);
$domDoc = new DOMDocument('1.0', 'UTF-8');
$domDoc->preserveWhiteSpace = false;
$domDoc->formatOutput = true;
$domDoc->encoding = 'UTF-8';
$domDoc->load('./data/expression.xml');
$xpath = new DOMXpath($domDoc);
if($domDoc->getElementsByTagName('expression')->length>0){
// If we already have expression tag defined
$expression = $domDoc->getElementsByTagName('expression')[0];
}else{
// If we don't have any expression tag, i.e. file is empty
$expression = $domDoc->createElement('expression');
}
$vocabulario = $domDoc->createElement('vocabulario');
$vocabulario->setAttribute('word', $_POST['word']);
$classe = $domDoc->createElement('classe', $_POST['classe']);
$domDoc->appendChild($expression);
$expression->appendChild($vocabulario);
$vocabulario->appendChild($classe);
file_put_contents('./data/expression.xml', $domDoc->saveXML());
header('location:index.php');
}
?>
<form method="post">
<div class="col-75">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.css" rel="stylesheet" />
<select name="classe[]" multiple="multiple">
<option value="| adjective |">adjective</option>
<option value="| adverb |">adverb</option>
<option value="| noun |">noun</option>
<option value="| verb |">verb</option>
</select>
<script type="text/javascript">
var s2 = $("#classe").select2({
placeholder: "Select",
tags: true
});
</script>
</div>
<div class="row">
<td><input type="submit" value="Save" name="submitSave"></td>
</div>
代码工作对我来说是正确的。
/home/vagrant/foo/index.php:3:
array (size=2)
'classe' =>
array (size=2)
0 => string '| adjective |' (length=13)
1 => string '| adverb |' (length=10)
'submitSave' => string 'Save' (length=4)
在PHP中有一些特殊的配置吗?
我的测试。
<?php
var_dump($_POST);
?>
<form method="POST">
<div class="col-75">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.css" rel="stylesheet"/>
<select id="classe" name="classe[]" multiple name="event_type[]" class="form-control" required>
<option value="| adjective |">adjective</option>
<option value="| adverb |">adverb</option>
<option value="| noun |">noun</option>
<option value="| verb |">verb</option>
</select>
<script type="text/javascript">
var s2 = $("#classe").select2({
placeholder: "Select",
tags: true
});
</script>
</div>
<div class="row">
<td><input type="submit" value="Save" name="submitSave"></td>
</div>
</form>
编辑。
当你在PHP中接收数组数据时,在你的情况下。
<select name="classes[]" >
PHP的变量是 $_POST['classes']
不 $_POST['classes[]']
而且它是一个数组,如果你想把它作为一个字符串使用,你必须使用 内爆