2010-08-12 78 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