2015-02-10 31 views
0

JavaScript數組這是我的HTML代碼如何創建HTML表單數組元素

<table width="62%" height="70" border="1" cellpadding="0" cellspacing="0" id="edit">    
    <?php if(count($voucher_info) > 0){ ?> 
     <tr class="bgcolor_02"> 

      <td width="27%" align="center" class="admin" >S.no</td> 
      <td width="37%" align="center" class="admin" >Voucher Type</td> 
      <td width="47%" align="center" class="admin" >Voucher Mode</td> 



     </tr> 
     <?php 
     $rownum = 1;  
     foreach($voucher_info as $eachrecord){ 
      $zibracolor = ($rownum%2==0)?"even":"odd"; 
      ?> 
      <tr align="center" class="narmal"> 
       <td height="25"><?php echo $eachrecord->voucher_id ; ?><input type="hidden" name="voucher_id[]" value="<?php echo $eachrecord->voucher_id; ?>" /></td> 
       <td><input name="vouchertype[]" type="text" value="<?php echo $eachrecord->voucher_type; ?>" id="vouchertype"/></td> 
       <td><select name="mode[]" > 
         <option value="paidin" <?php if($eachrecord->voucher_mode=='paidin'){ ?> selected="selected" <?php } ?>>Paid In</option> 
         <option value="paidout" <?php if($eachrecord->voucher_mode=='paidout'){ ?> selected="selected" <?php } ?>>Paid Out</option> 
        </select></td>     
      </tr> 
      <?php 
      } 
      }     
    else{ 
     echo "<tr class='bgcolor_02'>"; 
     echo "<td align='center'><strong>No records found</strong></td>"; 
     echo "</tr>"; 
    } 
    ?> 

</table> 
<input id="update" type="submit" name="submit" value="Edit"/> 

我只是想知道我怎麼訪問vouchertype和模式在JavaScript中,並通過他們使用Ajax控制器。我想將更新的值保存到數據庫。請有人有任何想法,然後告訴我。

+1

您可以使用Ajax提交後到PHP和處理它有 – 2015-02-10 05:09:08

+0

您可以隨時AJAX發佈Javascript數組,並在服務器端,你可以收集。例如你有一個數組myArr = [「Sunday」,「Monday」,「Tuesday」]'只需使用ajax發送'$ .ajax({url:「/ post.php」,type:'POST', data:{theArray:myArr}},success:function(resp){alert(JSON.stringify(resp));}})'在服務器上,你會像'$ thearray = $ _POST ['theArray']; '。你試過這個嗎?如果不是AJAX,那麼嘗試只是正常發佈表單? – amitthk 2015-02-10 06:02:10

+0

你試過[PHP:foreach](http://php.net/manual/en/control-structures.foreach.php)嗎?像這樣:'foreach($ mode_value as $ value){echo「Value:$ value
\ n」;/*在這裏插入$值* /}'。 – amitthk 2015-02-10 06:11:57

回答

0

首先,您的HTML無效,因爲每個元素ID在文檔中必須是唯一的。如果不是,則不能使用該ID來選擇元素。

在此代碼中刪除ID或爲每行生成唯一的ID。

<input name="vouchertype[]" type="text" value="<?php echo $eachrecord->voucher_type; ?>" id="vouchertype"/> 

<input name="vouchertype[]" type="text" value="<?php echo $eachrecord->voucher_type; ?>"/> 

Select the fields by name和遍歷集合來獲取值。

function saveValues(){ 
    var types = $('input[name="vouchertype[]"]'), 
    modes = $('select[name="mode[]"]'), 
    typeValues = [], modeValues = []; 
    types.each(function(el){ 
     typeValues.push(el.val())}) 
    modes.each(function(el){ 
     modeValues.push(el.val() ? el.val()[0] : false)}) 
    $.ajax(
     data: {vouchertype: typeValues, mode: modeValues}, 
     ... 
+0

您創建一個保存按鈕並附加此函數作爲處理程序。 – 2015-02-10 12:37:45

+0

完全沒有提示意味着之前必須有錯誤。您在顯影工具的控制檯中看到什麼?有沒有錯誤? – 2015-02-10 13:02:28

+0

你說得對,我們需要引用元素名稱。我也改變了相關問題的鏈接。 – 2015-02-10 13:11:29