【发布时间】:2020-02-20 02:31:44
【问题描述】:
我在 html 中有代码,通过在组合框中选择数据并单击添加按钮来在列表中添加数据。提交表单时遇到错误“未定义的索引:主题列表”。感谢您的建议?谢谢你。我仍然是网络编程的新手。
<h4>Subjects</h4>
<ul class="list-group list" id='subjectlist' name='subjectlist' >
</ul>
<div class="form-group">
<input class="submit" name="submit" type="submit" value="Save">
</div>
</form>
<button class="add_field_button" onclick="getsubject()">Add Subject</button>
<button class="add_field_button" onclick="removesubject()">Remove Subject</button>
<script>
function getsubject(){
var ul = document.getElementById("subjectlist");
var candidate = document.getElementById("st");
var SelectedValue = candidate.options[candidate.selectedIndex].text;
var li = document.createElement("li");
li.setAttribute('class',"list-group-item");
li.setAttribute('id',"subjectlistitem");
li.appendChild(document.createTextNode(SelectedValue));
ul.appendChild(li);
}
</script>
<?php
if(isset($_POST['submit'])){ // Fetching variables of the form which travels in URL
$sectioncode = $_POST['sc'];
$sectiongroup = $_POST['ccg'];
$selectedsubject = $_POST['st'];
$subjectlist = $_POST['subjectlist'];
$i = 0;
foreach ($subjectlist as $qst) {
$sql = "INSERT INTO sectionsubject(sectioncode, subjectcoden) VALUES ('" . $csectioncode . "',
'" . $subjectlist[$i] . "')";
if ($link->query($sql) === TRUE) {
echo "success";
} else {echo "error" . $link->error;}
$i++;}
}
?>
【问题讨论】:
-
li元素不会作为表单字段提交。仅限input、select和textarea。 -
嗨,Barmar,您能就这方面的任何解决方法提出建议吗?谢谢。
-
使用多选输入,而不是将选定的值复制到列表中。
-
感谢 Barmar,将改用多选输入。
标签: javascript php html list