2016-05-16 128 views
-6

我有一個帶有複選框的表格,我想編輯一些td並想獲取值。 這裏是我的代碼如何從​​獲取值並保存到php的數據庫

<table class="table table-bordered table-striped table-hover"> 
    <thead> 
     <tr> 
      <th><input type="checkbox" id="allcb" name="allcb"/> Check all</th> 
      <th data-field="id">ID</th> 
      <th data-field="firstName">First name</th> 
      <th data-field="lastName">Last name</th> 
     </tr> 
    </thead> 
    <tbody> 
     {% for user in product.users %} 
       <td><input type="checkbox" name="checkBox[]" id="ic" value="{{ user.id }}" /></td> 
       <td data-title="id">{{user.id}}</td> 
       <td data-title="firstName">{{user.firstname}}</td> 
       <td data-title="lastName" contenteditable='true' name="v1">{{user.lastname}}</td> 

      </tr> 
     {% endfor %} 
    </tbody> 

</table> 
if(isset($_POST['checkBox']) && !empty($_POST['checkBox'])&& isset($_POST['v1'])){ 

     $user =$_POST['checkBox']; 
     $v1 =$_POST['v1']; 

     $zl = 0; 
     foreach ($user as $id) 
     { 
      foreach ($v1 as $v2) 
      { 

      $userd = $em->getRepository('UserBundle:User')->find($id); 
      $userd->setlastName($v2); 
      $em->persist($userd); 


      ++$zl; 
      }} 

     $em->flush(); 
     } 

我沒有得到任何結果。我試圖得到這樣的值: $ v1 = $ _ POST ['v1'];但我沒有得到任何東西。

有人可以幫助我嗎?謝謝!

+2

您是否在問如何提交表單? –

+0

我想從 {{user.lastname}} –

回答

0

您至少有兩個選項:

隱藏字段無處不在,如果數據(最好的方法):

<td> 
    <input type="checkbox" name="checkBox[]" id="ic" value="{{ user.id }}" /> 
    <input type="hidden" value="{{user.lastname}}" name="v1[{{ user.id }}]"></td> 
<td data-title="id">{{user.id}}</td> 
<td data-title="firstName">{{user.firstname}}</td> 
<td data-title="lastName" contenteditable='true'>{{user.lastname}}</td> 

,並在服務器端你做:

.... your code .... 
foreach ($user as $id) { 
    $lastname = $v1[$id]; 
    ... save your lastname value ... 
} 

Anothe選擇是設置複選框值與數據分隔的某個值(醜陋的解決方案)

<td><input type="checkbox" name="checkBox[]" id="ic" value="{{ user.id }}#{{user.lastname}}" /></td> 
      <td data-title="id">{{user.id}}</td> 
      <td data-title="firstName">{{user.firstname}}</td> 
      <td data-title="lastName" contenteditable='true' name="v1">{{user.lastname}}</td> 

編輯

我現在正在使用CONTENTEDITABLE看到。您應該將該值添加到您的隱藏字段中:

$("form").submit(function() { 
    $('input[name*="v1"]').val($(this).closest('td[name="v1"]').html()); 
}); 

<td> 
    <input type="checkbox" name="checkBox[]" id="ic" value="{{ user.id }}" /> 
    <input type="hidden" value="{{user.lastname}}" name="v1[{{ user.id }}]"></td> 
<td data-title="id">{{user.id}}</td> 
<td data-title="firstName">{{user.firstname}}</td> 
<td data-title="lastName" contenteditable='true' name="v1">{{user.lastname}}</td> 
+0

謝謝。我得到以前的值我沒有得到編輯值:/ –

+0

編輯。您需要在提交表單之前用新數據填充隱藏字段。 –

相關問題