为什么我的动态添加<select>元素不会通过POST发送?</select>

时间:2012-06-14 22:17:04

标签: jquery forms post

以下是我的表格 使用jQuery我根据之前<select>的值加载<select>元素。但是,当我发送表单时,动态添加的<select>(add_student_contact_id)的值不是$_POST'。是因为它在页面加载后添加了吗?有什么办法可以解决吗?

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";
}

1 个答案:

答案 0 :(得分:1)

一个可能的原因是$row['contact_id']在注入页面时没有值。另一个原因是您缺少name属性。这意味着浏览器要么是:

  • 发送空白值
  • 完全省略

如果您可以检查并发回,我们可以提供更多帮助。