Below is my form.
Using jQuery I load a <select> element based on value on previous <select>. However when I send the form the value of the dynamically added <select> (add_student_contact_id) is not beeing $_POST‘ed. Is it because it’s beeing added after the page load? Is there any way to solve it?
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";
}
One possible reason is that
$row['contact_id']doesn’t have a value when injected into the page. Another reason is you are missing anameattribute. This would mean the browser is either:If you could check that and post back we could provide some more help.