2015-02-10 33 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