2013-06-25 90 views
0

我正在使用jQuery更改事件來填充下拉菜單,當用戶選擇正確的公司。換句話說,它用數據填充第二個菜單。雖然這是正常工作,但當我發佈表單並使用序列化捕獲數據時,該帖子中沒有公司或地址詳細信息。只是選項的價值。在這種情況下,「地址」。我如何編碼,以便可以收集使用此方法發佈的數據。由於jquery select change event not posting form data

jQuery代碼

$(function() { 

     $("#BA_customer").live('change', function() { 
      if($(this).val()!="") 
      $.get("/domain/admin/getDept.php?BA_customer=" + $(this).val(), function(data) { 
      $("#BA_dept").html(data).show(); 
      }); 
      $.get("/domain/admin/getOptions.php?BA_customer=" + $(this).val(), function(data) { 
      $("#BA_address").html(data).show(); 
      }); 

     }); 
      }); 


$(function(){   
     $("#BA_boxform").submit(function(){ 

     var formdata = $('#BA_boxform').serialize(); 
     //alert(formdata); 
     $.ajax({ 
      type: "POST", 
      url: "/domain/admin/requests/boxes/boxesadd.php", 
      data: formdata, 
      dataType: 'json', 
      success: function(msg){ 
       //$("#confirm_department").hide(); 

       /* 
       var $dialog = $('<div id="dialog"></div>') 
       .html('Your intake was successfully submitted and will be viewable in the reporting area.<br /><br />Thank you.'); 
       $dialog.dialog({ 
       autoOpen: true, 
       modal: true, 
       title: 'Box intake submission successfull', 
       width: 400, 
       height: 200, 
       draggable: false, 
       resizable: false, 
       buttons: { 
       Close: function() { 
       $(this).dialog("close"); 
       } 
       } 
       }); 
       */ 
       //alert('You have succesfully submitted your ' + msg.company + ' report. Thank you.'); 
       //console.log(msg); 
       //$("#BA_addbox").html("You may now close this window."); 

       //$("#formImage .col_1 li").show(); 
       $("#BA_boxform").get(0).reset(); 
       $("#boxaddform").hide(); 
      } 
     }); 
     return false; 
    }); 
}); 

// End function to submit box intake form 

getOptions.php代碼

 echo '<label for="address">Address:</label>'.'<br />'.'<select name="customeraddress">'; 
     echo '<option value="">Select delivery address</option>'; 
     while ($row_rs_select_address2 = mysql_fetch_assoc($rs_select_address2)) 
     { 
     $address=$row_rs_select_address2['address1_com']. "". 
     $row_rs_select_address2['address2_com']. "". 
     $row_rs_select_address2['address3_com']. " ". 
     $row_rs_select_address2['town_com']. " ". 
     $row_rs_select_address2['postcode_com']; 
     echo '<option value="address">'.$address.'</option>'; 
     } 
     echo '</select>'; 
+2

.live已被棄用。您應該將其更改爲.on - 幾乎相同的語法。 – gibberish

+0

只是問 - 第一個'.get'包裹在'if'中,但是第二個不是,那很好嗎? – Shaddow

回答

4

在這裏,你是很難循環的每次迭代編碼選項值address

echo '<option value="address">'.$address.'</option>'; 

那意味着選擇了哪個地址,服務器端只會看到「地址」。相反,您應該將地址ID插入到值中。

echo '<option value="' . (int)$row_rs_select_address2['address_id'] . '">'.$address.'</option>'; 
// assuming the ID is in $row_rs_select_address2['address_id'] 
+0

非常感謝您的幫助 – user1532468