2016-08-02 82 views
0

我正在建立一個表單來創建發票。目前它的表格字段爲:動態發票表單字段和發佈到數據庫

產品名稱,數量。

我有一個「添加更多」按鈕,它添加了上述表單字段的另一行。

然後它作爲一個數組提交給另一個php頁面。我遇到的問題是匹配每個相關聯的表單字段,因爲當我將其保存在數據庫中時,我需要正確的產品名稱,數量顯然與表格中的同一行相同。只是不知道如何匹配他們。

目前我的表單域頁面上的代碼是:

<html> 
     <head> 
      <title>Invoice Test</title> 
      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" /> 

      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script> 
      <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script> 
     </head> 
     <body> 
      <div class="container"> 
       <br /> 
       <br /> 
       <h2 align="center">Invoice Test</h2> 
       <div class="form-group"> 
        <form name="submit" id="submit" action="name.php" method="POST"> 
          <div class="table-responsive"> 
           <table class="table table-bordered" id="dynamic_field"> 
            <tr> 
             <td><input type="text" name="product_name[]" placeholder="Product Name" class="form-control name_list" /></td> 
             <td><input type="text" name="qty[]" placeholder="QTY" class="form-control name_list" /></td> 
             <td><button type="button" name="add" id="add" class="btn btn-success">Add More</button></td> 
            </tr> 
           </table> 
           <input type="submit" name="submit" id="submit" class="btn btn-info" value="Submit" /> 
          </div> 
        </form> 
       </div> 
      </div> 
     </body> 
</html> 
<script> 
$(document).ready(function(){ 
     var i=1; 
     $('#add').click(function(){ 
      i++; 
      $('#dynamic_field').append('<tr id="row'+i+'"><td><input type="text" name="product_name[]" placeholder="Product Name" class="form-control name_list" /></td><td><input type="text" name="qty[]" placeholder="QTY" class="form-control name_list" /></td><td><button type="button" name="remove" id="'+i+'" class="btn btn-danger btn_remove">X</button></td></tr>'); 
     }); 
     $(document).on('click', '.btn_remove', function(){ 
      var button_id = $(this).attr("id"); 
      $('#row'+button_id+'').remove(); 
     }); 
}); 
</script> 

在那裏的形式提交給name.php頁面。從數組中獲取每個產品和相關數量並將其放入數據庫行的最佳方式是什麼?牢記這是動態的。

在name.php當我打印出$ _ POST目前,我有這個陣列結構:

Array 
(
    [product_name] => Array 
     (
      [0] => test1 
      [1] => test2 
      [2] => test3 
      [3] => test4 
     ) 

    [qty] => Array 
     (
      [0] => 1 
      [1] => 2 
      [2] => 3 
      [3] => 4 
     ) 

    [submit] => Submit 
) 

即時猜測我需要算鍵,然後在陣列的另一維度匹配。只是不確定這是否是最好的方式,甚至不知道如何做到這一點。

回答

1

您可以使用此

$productName = $_POST['productName']; 
$qty = $_POST['qty'] 

foreach($productName as $key => $productName) { 
    $productQty = $qty[$key]; 

    //Use your ORM (or whatever) to inster into DB 
    //$productName and $productQty 

} 

編輯

,或者甚至更好,你可以用戶PHP的array_combine

$productName = $_POST['productName']; 
$qty = $_POST['qty'] 

$arryWithTotals = array_combine($productName, $qty); 

foreach($arrayWithTotals as $productName => $productQty) { 
    //insert into DB 
} 
+1

我的上帝。我去尋找一些在php手冊中做過的事情。一定忽略了它。非常感謝您花時間回答。這正是我需要並且非常容易實現的。 我會很快接受答案,當它讓我。 – Elgoots

+0

我不得不改變它,因爲我結束了4個表單字段和4個數組。我有一個新問題。如果您在單個價格字段中輸入數字,然後輸入數字,您將注意到總價格字段和稅務字段會更新所需的數學輸出。 即時通訊的問題是,它不會做任何其他人,除了最初那裏的人。即時通訊不知道如何使它在動態表單字段中添加數學:http://pastebin.com/3pH3CX88 – Elgoots

0
If you wish you can use like these way. 

<html> 
 
<head> 
 
    <title>Invoice Test</title> 
 
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" /> 
 

 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script> 
 
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script> 
 
</head> 
 
<body> 
 
<div class="container"> 
 
    <br /> 
 
    <br /> 
 
    <h2 align="center">Invoice Test</h2> 
 
    <div class="form-group"> 
 
     <form name="submit" id="submit" action="name.php" method="POST"> 
 
      <div class="table-responsive"> 
 
       <table class="table table-bordered" id="dynamic_field"> 
 
        <tr> 
 
         <td><?php 
 
          session_start(); 
 

 
          if (isset($_SESSION["message"]) && !empty($_SESSION["message"])) { 
 
           echo $_SESSION["message"]; 
 
           unset($_SESSION["message"]); 
 
          } 
 

 
          ?></td> 
 
         <td><input type="text" name="product_name[]" placeholder="Product Name" class="form-control name_list" /></td> 
 
         <td><input type="text" name="qty[]" placeholder="QTY" class="form-control name_list" /></td> 
 
         <td><button type="button" name="add" id="add" class="btn btn-success">Add More</button></td> 
 
        </tr> 
 
       </table> 
 
       <input type="submit" name="submit" id="submit" class="btn btn-info" value="Submit" /> 
 
      </div> 
 
     </form> 
 
    </div> 
 
</div> 
 
</body> 
 
</html> 
 
<script> 
 
    $(document).ready(function(){ 
 
     var i=1; 
 
     $('#add').click(function(){ 
 
      i++; 
 
      $('#dynamic_field').append('<tr id="row'+i+'"><td><input type="text" name="product_name[]" placeholder="Product Name" class="form-control name_list" /></td><td><input type="text" name="qty[]" placeholder="QTY" class="form-control name_list" /></td><td><button type="button" name="remove" id="'+i+'" class="btn btn-danger btn_remove">X</button></td></tr>'); 
 
     }); 
 
     $(document).on('click', '.btn_remove', function(){ 
 
      var button_id = $(this).attr("id"); 
 
      $('#row'+button_id+'').remove(); 
 
     }); 
 
    }); 
 
</script>

PHP 
==== 

$data = $_POST['product_name']; 
$data2 = $_POST['qty']; 

$product_name = "'" . implode("','", array_values($data)) . "'"; 
$qty = "'" . implode("','", array_values($data2)) . "'"; 

$conn = mysql_connect("localhost", "root", "") or die("unable to connect Mysql"); 
mysql_select_db("Your DB Name called here") or die("unable to connect DB"); 

$query = "INSERT INTO `Your DB Name called here`.`Your Table Name called here` (`id`, `product_name`,`qty`) VALUES (NULL, '$product_name', '$qty')"; 
if (mysql_query($query)) { 
    $_SESSION["message"] = "Successfully submitted"; 
} 
header("location:your page name.php");