0
以下是我的表格。
使用jQuery我根據之前的<select>
上的值加載<select>
元素。但是,當我發送表單時,動態添加的<select>
(add_student_contact_id)的值不是$_POST
'ed。這是因爲它是在頁面加載後添加的嗎?有什麼辦法解決它?爲什麼我的POST動態地添加了<select>元素?
students.php
<form action='../insert.php' method='post' name='add_student'>
<!-- This is generated by PHP -->
<select id='add_student_customer'>
<option>Select one...</option>
<option value='1'>First value</option>
<option value='2'>Second value</option>
<option value='3'>Third value</option>
</select>
<!-- Here a select element will load from another PHP-file based on values from the previous select -->
<div id='add_student_contactperson_container'></div>
<!-- This is generated on page load but will not be visible until we select something in the previous select -->
<select id='add_student_course'>
<option>Select something...</option>
<option value='1'>An option</option>
<option value='2'>A second option</option>
</select>
</form>
的jQuery
<script>
$(document).ready(function() {
$("#add_student_course_container").hide();
$(document).on("change", "#add_student_customer_select", function(){
var row_id = $(this).val();
$.ajax({
url: "/load_customer_contacts.php",
data: { 'customer_id':row_id },
success: function(data){
$("#add_student_contactperson_container").html(data);
}
});
});
});
$(document).on("change", "#add_student_contact", function(){
$("#add_student_course_container").show();
});
</script>
/load_customer_contacts.php
if (isset($_GET['customer_id'])) {
/* SQL stuff */
echo "<select name='add_student_contact_id'>\n";
while ($row = mysql_fetch_assoc($sql)) {
echo "<option value='".$row['contact_id']."'>".$row['name']."</option>\n";
}
echo "</select>\n";
}
我認爲你需要爲'select'元素設置'name'屬性。 – mschonaker
已添加。感謝您的通知。 – David