2017-09-04 129 views
0

我想要創建一個表,其中每個行都可以編輯它。同時,我希望能夠將所有行數據放入一個文件中。發送關聯數組中的所有數據的發佈請求

我遇到的問題是當我點擊id = download的按鈕時,我無法獲取所有行中的所有數據,但只有第一行,就好像我點擊了id = firstRow的提交按鈕。

任何人都知道如何通過單擊一個按鈕來檢索所有數據行? 我現在只是使用PHP,所以使用PHP或HTML解決這個問題會有很大的幫助。

<form method="post" action="allRows.php"> 
    <table border="1"> 
     <tr> 
      <th>Name</th> 
      <th>Age</th> 
      <th>Gender</th> 
      <th>Single</th> 
      <th>Test button</th> 
     </tr> 
     <tr> 
      <form action="oneRow.php" method="post"> 
       <td><input type="text" name="person[0][name]"></td> 
       <td><input type="text" name="person[0][age]"></td> 
       <td><input type="text" name="person[0][sex]"></td> 
       <td><input type="text" name="person[0][spouse]"></td> 
       <td><input id="firstRow" type="submit" name="test"></td> 
      </form> 
     </tr> 
     <tr> 
      <form action="oneRow.php" method="post"> 
       <td><input type="text" name="person[1][name]"></td> 
       <td><input type="text" name="person[1][age]"></td> 
       <td><input type="text" name="person[1][sex]"></td> 
       <td><input type="text" name="person[1][spouse]"></td> 
       <td><input id="secondRow" type="submit" name="test"></td> 
      </form> 
     </tr> 
    </table> 
    <input id="download" type="submit" name="Download" value="Download"> 
</form> 
+0

只能使用1'

'標籤。 –

+0

通過僅使用1個窗體標籤,我無法單獨編輯每個單獨的行。 –

回答

1

你不能表單內嵌套形式,也就是無效的HTML,所以如果你想擁有所有這些功能,您將需要使用的JavaScript由它們組裝成一個js表單提交一次所有的形式但純粹的PHP是不可能的,除非你一次提交一個包含所有字段的表單。

<!-- You need the jQuery library --> 
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> 
    <table border="1"> 
     <tr> 
      <th>Name</th> 
      <th>Age</th> 
      <th>Gender</th> 
      <th>Single</th> 
      <th>Test button</th> 
     </tr> 
     <tr> 
      <!-- You need to add class="rowform" to each form tag --> 
      <form action="oneRow.php" method="post" class="rowform"> 
       <td><input type="text" name="person[0][name]"></td> 
       <td><input type="text" name="person[0][age]"></td> 
       <td><input type="text" name="person[0][sex]"></td> 
       <td><input type="text" name="person[0][spouse]"></td> 
       <td><input id="firstRow" type="submit" name="test"></td> 
      </form> 
     </tr> 
     <tr> 
      <form action="oneRow.php" method="post" class="rowform"> 
       <td><input type="text" name="person[1][name]"></td> 
       <td><input type="text" name="person[1][age]"></td> 
       <td><input type="text" name="person[1][sex]"></td> 
       <td><input type="text" name="person[1][spouse]"></td> 
       <td><input id="secondRow" type="submit" name="test"></td> 
      </form> 
     </tr> 
    </table> 
<!-- Create the empty download all form, add id="allrows" --> 
<form method="post" action="allRows.php" id="allrows"> 
    <input id="download" type="submit" name="Download" value="Download"> 
</form> 

<script> 
// Start document listener 
$(document).ready(function(e) { 
    // Listen for the download form to submit 
    $('#allrows').on('submit',function(e) { 
     // Stop it from reloading the page (submitting the form) 
     e.preventDefault(); 
     // Create a storage array 
     var data = []; 
     // Loop through each form (each form tag needs the "rowform" class 
     $.each($('.rowform'),function(k,v) { 
      // Fetch all the data from the form 
      data[k] = $(v).serialize(); 
     }); 
     // Create a storage array for the form 
     var form = []; 
     // Start building a form 
     form.push('<form action="allRows.php" method="post">'); 
     // Implode the form data from each form 
     form.push('<input name="allfields[]" value="'+data.join('" /><input name="allfields[]" value="')+'" />'); 
     // Create a submit field 
     form.push('<input type="submit" value="submit" /></form>'); 
     // Combine the html form 
     form = form.join(''); 
     // Submit the form 
     $(form).submit(); 
    }); 
}); 
</script> 

在PHP中,你將需要檢查的allfields鍵,以便:

if(!empty($_POST['allfields'])) { 
    // do code 
} 

您將看到的是一樣的東西:

Array 
(
    [allfields] => Array 
     (
      [0] => person%5B0%5D%5Bname%5D=qewrqwer&person%5B0%5D%5Bage%5D=adsf&person%5B0%5D%5Bsex%5D=fdsdfds&person%5B0%5D%5Bspouse%5D=sdfds 
      [1] => person%5B1%5D%5Bname%5D=sssssss&person%5B1%5D%5Bage%5D=sssweeeee&person%5B1%5D%5Bsex%5D=qqqqqq&person%5B1%5D%5Bspouse%5D=222222 
     ) 
) 

你會看到該字段有一系列數組和查詢字符串。使用urldecode()等處理您想要的方式。

+0

我當然需要了解更多關於Javascript的知識。現在,我通過編寫另一組隱藏的輸入並一次檢索所有數據並將其放入表單中。通過這樣做可以避免嵌套形式。 –