2012-06-22 77 views
0

我有一個表單,其中單擊添加更多按鈕時出現新的字段。我如何將它們存儲到php變量中,然後放入mysql數據庫中。 我有PHP和MySQL的工作知識。 我的HTML和JavaScript代碼如下:在陣列如何爲動態生成的輸入字段添加記錄

$_POST

<script type="text/javascript"> 
    function($) { 

$.fn.relCopy = function(options) { 
    var settings = jQuery.extend({ 
     excludeSelector: ".exclude", 
     emptySelector: ".empty", 
     copyClass: "copy", 
     append: '', 
     clearInputs: true, 
     limit: 0 // 0 = unlimited 
    }, options); 

    settings.limit = parseInt(settings.limit); 

    // loop each element 
    this.each(function() { 

     // set click action 
     $(this).click(function(){ 
      var rel = $(this).attr('rel'); // rel in jquery selector format    
      var counter = $(rel).length; 

      // stop limit 
      if (settings.limit != 0 && counter >= settings.limit){ 
       return false; 
      }; 

      var master = $(rel+":first"); 
      var parent = $(master).parent();       
      var clone = $(master).clone(true).addClass(settings.copyClass+counter).append(settings.append); 

      //Remove Elements with excludeSelector 
      if (settings.excludeSelector){ 
       $(clone).find(settings.excludeSelector).remove(); 
      }; 

      //Empty Elements with emptySelector 
      if (settings.emptySelector){ 
       $(clone).find(settings.emptySelector).empty(); 
      };        

      // Increment Clone IDs 
      if ($(clone).attr('id')){ 
       var newid = $(clone).attr('id') + (counter +1); 
       $(clone).attr('id', newid); 
      }; 

      // Increment Clone Children IDs 
      $(clone).find('[id]').each(function(){ 
       var newid = $(this).attr('id') + (counter +1); 
       $(this).attr('id', newid); 
      }); 

      //Clear Inputs/Textarea 
      if (settings.clearInputs){ 
       $(clone).find(':input').each(function(){ 
        var type = $(this).attr('type'); 
        switch(type) 
        { 
         case "button": 
          break; 
         case "reset": 
          break; 
         case "submit": 
          break; 
         case "checkbox": 
          $(this).attr('checked', ''); 
          break; 
         default: 
          $(this).val(""); 
        }      
       });     
      }; 

      $(parent).find(rel+':last').after(clone); 

      return false; 

     }); // end click action 

    }); //end each loop 

    return this; // return to jQuery 
}; 

    })(jQuery); 

    </script> 

    <script type="text/javascript"> 
    $(function(){ 
    var removeLink = ' <a class="remove" href="#" onclick="$(this).parent().slideUp(function(){ $(this).remove() }); 
return false">remove</a>'; 
$('a.add').relCopy({ append: removeLink}); 
}); 
    </script> 
    <style type="text/css"> 
    body{ font-family:Arial, Helvetica, sans-serif; font-size:13px; } 
    .remove {color:#cc0000} 
    .input{ border: solid 1px #006699; padding:3px} 

    </style> 
    </head> 

    <body> 
    <form method="post" action=""> 
    <table width="580" border="1" class="clone" rules="groups" align="center"> 
    <tr> 
    <td width="129" height="29" align="right"><strong>Child Name :</strong></td> 
    <td width="435"><input name="petname[]" type="text" class="txt_box" id="petname" /> 
    </td> 
    </tr> 
    <tr> 
    <td align="right" height="29"><strong>Pet Image :</strong></td> 
    <td><input name="petimage[]" type="file" class="txt_box" id="petimage" /></td> 
    </tr> 

    </table> 
    <p style=" margin-left:60px;"><a href="#" class="add" rel=".clone">Add More</a></p> 
    </form> 
    </body> 
    </html> 
+0

你嘗試過這麼遠嗎?順便說一句。這可能會變得相當複雜(我曾經沿着這些線寫過),所以確保你有一個堅實的數據庫(確保你的ERD是正確的等) – Bono

+0

@Bono DB沒有問題,我只是想知道如何讓他們在PHP中,即以數組的形式,或者如果有別的東西。 – user995426

回答

0

一旦表單被提交,你可以使用get發佈數據的值。

然後,您將需要迭代要插入到數據庫中的數組。

一個例子:

<?php 
    $con = mysql_connect("localhost","username","password"); 
    if (!$con) 
     { 
     die('Could not connect: ' . mysql_error()); 
     } 

    mysql_select_db("my_db", $con); 

    mysql_query("INSERT INTO tablename(FirstName, LastName, Age) 
    VALUES ('test', 'name',25)"); 
    mysql_close($con); 
?> 
+0

我知道$ _POST是一個數組,我想知道如何從輸入字段獲取值並添加到數據庫中 – user995426

+0

只需打印$ _POST數組並檢查結果。 如果動態生成的輸入字段本身是一組輸入字段,也就是它本身是一個數組,那麼您可以使用$ _POST ['values']獲取值,然後使用上面的語句 – Vimalnath

+0

@vimalnath插入數據庫,然後$ _post變成數組陣列,請您指出這種情況來描述答案.. – harry