0

This is my html code

  <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"/>

I just want to know how do I access vouchertype and mode in javascript and pass them to controller using ajax. I want to save updated values to database. Please anybody has any idea, then tell me.

Kedar B
  • 774
  • 5
  • 18
  • 47
  • 1
    you can use ajax to submit the post to php and process it there – Oli Soproni B. Feb 10 '15 at 05:09
  • You can always ajax post the Javascript array and at server-side you can collect. e.g you have an array `myArr = ["Sunday","Monday","Tuesday"]` just use the ajax to send it `$.ajax({url:"/post.php",type:'POST', data:{theArray:myArr }}, success:function(resp){alert(JSON.stringify(resp));}})` On server you'd get it like `$thearray = $_POST['theArray']; ` . Have you tried this? If not AJAX then try to just normal post the form? – amitthk Feb 10 '15 at 06:02
  • Have you tried [PHP:foreach](http://php.net/manual/en/control-structures.foreach.php)? Like this: `foreach ($mode_value as $value) {echo "Value: $value
    \n"; /*Insert the $value here*/ }`.
    – amitthk Feb 10 '15 at 06:11

1 Answers1

0

To start with, your HTML is not valid, because each element id must be unique in the document. If it isn't, you cannot use that id for selecting the elements.

In this code remove the id or generate a unique id for each row.

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

like

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

Select the fields by name and iterate over the collections to get the values.

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},
        ...
Community
  • 1
  • 1
Lorenz Meyer
  • 19,166
  • 22
  • 75
  • 121