因此,我有一个 HTML/PHP 表单,它有一个选择列表框,您可以从中选择多个值,因为它的多个属性设置为多个。考虑表单方法是“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>
慕标琳琳