2010-08-12 47 views
0

我構建了一個頁面,當您單擊「添加產品」時,它會自動生成新的div。每個div都包含一個具有唯一名稱和ID的表單。我用這個PHP和AJAX多級表單教程來製作我的表單 - http://www.codingcereal.com/2009/09/autopopulate-select-dropdown-box-using-jquery/無法從動態php和ajax表單調用發佈的值

問題是,我似乎無法通過PHP提交表單時調用值。我能想到的唯一原因是與動態生成的表單有關。

任何想法?讓我知道你是否需要更多信息。

 var i = 0; 

    $('a#add-product').click(function(event){ 
     i++; 
     $('<div />').addClass('product').attr('id', 'product'+i) 
      .append($('<h2><img src="<?php echo base_url();?>img/product.png" alt="" />Product '+i+'</h2>')) 
      .append($('<div class="info-line"><label>Division</label><p><select id="selection-'+i+'"><option value="">- Select a Division -</option><option value="abrasives">Abrasives</option><option value="tapes">Bonding, Surface Protection &amp; Tapes</option><option value="packaging">Packaging</option></select></p></div>')) 
      .append($('<div class="info-line"><label>Category</label><p><select id="selectionresult-'+i+'"></select><span id="result-'+i+'">&nbsp;</span></p></div>')) 
      .append($('<div class="info-line"><label>Product</label><p><select id="selectionresult2-'+i+'"></select><span id="result2-'+i+'">&nbsp;</span></p></div>')) 
      .append($('<a class="remove" href="#add-product" id="remove-product'+i+'"><img src="<?php echo base_url();?>img/remove-product.jpg" alt="" />Remove Product</a>')) 
      .appendTo("#products"); 


      // START OF ADDITIONAL PRODUCT DROP DOWNS 

        $("#selectionresult-"+i).hide(); 
        $("#selectionresult2-"+i).hide(); 

        $("#selection-"+i).change(function() { 
         $("#selectionresult-"+i).hide(); 
         $("#selectionresult2-"+i).hide(); 
         $("#result-"+i).html('Retrieving ...'); 
         $.ajax({ 
          type: "POST", 
          data: "data=" + $(this).val(), 
          url: "<?php echo base_url();?>dropdown.php", 
          success: function(msg){ 
           if (msg != ''){ 
            $("#selectionresult-"+i).html(msg).show(); 
            $("#result-"+i).html(''); 
           } 
           else{ 
            $("#result-"+i).html('<em>No item result</em>'); 
           } 
          } 
         }); 
        }); 
        $("#selectionresult-"+i).change(function() { 
         $("#selectionresult2-"+i).hide(); 
         $("#result2-"+i).html('Retrieving ...'); 
         $.ajax({ 
          type: "POST", 
          data: "data=" + $(this).val(), 
          url: "<?php echo base_url();?>dropdown.php", 
          success: function(msg){ 
           if (msg != ''){ 
            $("#selectionresult2-"+i).html(msg).show(); 
            $("#result2-"+i).html(''); 
           } 
           else{ 
            $("#result2-"+i).html('<em>No item result</em>'); 
           } 
          } 
         }); 
        }); 

      // END OF ADDITIONAL PRODUCT DROP DOWNS 

      //START OF PRODUCT IMAGE PREVIEWS 
       var productSelection = document.getElementById("selectionresult2-"+i); 
       var productPreview = document.getElementById("product"+i+"image"); 

       //Accessories 
       $('#selectionresult2-'+i).change(function() { 
        if (productSelection.value == "3M Disc Pad Face 1"){ 
         productPreview.src = "<?php echo base_url();?>img/3m-disc-pad1.jpg"; 
        } 
        else if (productSelection.value == "Belt 1"){ 
         productPreview.src = "<?php echo base_url();?>img/belt1.jpg"; 
        } 
        else { 
         productPreview.src = "<?php echo base_url();?>img/spacer.gif"; 
        } 
       }); 

       //Belts 
       $('#selectionresult2-'+i).change(function() { 
        if (productSelection.value == "3M Disc Pad Face 1"){ 
         productPreview.src = "<?php echo base_url();?>img/3m-disc-pad1.jpg"; 
        } 
        else if (productSelection.value == "Belt 1"){ 
         productPreview.src = "<?php echo base_url();?>img/belt1.jpg"; 
        } 
        else { 
         productPreview.src = "<?php echo base_url();?>img/spacer.gif"; 
        } 
       }); 


    }); 
+0

發佈您的代碼,以便我們可以爲您提供幫助。謝謝 – Youssef 2010-08-12 14:16:54

+0

發表。這很多,但它實際上是在頁面中添加了一個div,其中包含字段。每個字段都是基於'我' – Carson 2010-08-12 14:31:10

回答

0

如果生成每個格內一種新的形式,那麼當你提交的頁面,你就不會每次提交您所創建的形式,而只是形式提交按鈕所屬。

如果你沒有在每個div中添加一個新表單,那麼所有新的字段將屬於你提交的那個,你應該能夠獲得它們的值。

+0

我錯過了唯一。他們在每個div中都不是單獨的形式,而只是選擇字段,因此它們都屬於同一個表單。 – Carson 2010-08-12 14:10:16

+0

每個選擇選項是否都設置了值,即<選項值=「1」> 1,或者是缺少的值? 另外,當你做print_r($ _ POST)時你能看到提交頁面中的字段;假設表單是通過郵政發送的 – gabe3886 2010-08-12 14:30:36

+0

是的每個選項都在外部PHP文件中分配一個值。我測試是否通過電子郵件發送提交的值。我使用: $ prod1img = $ _POST ['selection-1']; 我試圖調用所有三個下拉菜單來查看是否有任何工作,但沒有工作。 – Carson 2010-08-12 14:33:46