YVS1102 YVS1102 - 22 days ago 6
jQuery Question

how to add semicolon from a text inside `<td>` then send it via ajax

I'm trying to send text from my

<td>
with my simple ajax to my controller (I'm using Codeigniter).

Here is my HTML

<table class="table table-striped col-md-9 cstm">
<thead>
<tr>
<td> <strong>Transaction No</strong> </td>
<td> <strong> Line No </strong> </td>
<td> <strong>Item Code</strong> </td>
<td> <strong>Item Name</strong> </td>
<td> <strong>Extra Remark</strong> </td>
<td> <strong>Extra Remark 2</strong> </td>
<td width="10%"> <strong>Item QTY</strong> </td>
<td width="10%"> <strong>Receive QTY</strong> </td>
<td width="10%"> <strong>UOM Code</strong> </td>
<td width="10%"> <strong>Extra Remark Receipt</strong> </td>
</tr>
</thead>

<tbody>
<tr>
<td class="notrans">DORD-DK-M-TAM-69</td>
<td>1000 <input value="1000" type="hidden" name="LineNo[]" /> </td>
<td>S160291 <input value="S160291" type="hidden" name="ItemCode[]" /> </td>
<td>SMALL BAG <input value="SMALL BAG" type="hidden" name="ItemName[]" /> </td>
<td> <input value="" type="hidden" name="ExtraRemark[]" /></td>
<td> <input value="" type="hidden" name="ExtraRemark2[]" /></td>
<td>2 <input value="2.00000000000000000000" type="hidden" name="Quantity[]" /> </td>
<td><input required="required" type="text" value="2" name="recv_qty[]" /> </td>
<td> PCS<input type="hidden" value="PCS" name="uom[]" /> </td>
<td><input type="text" value="" name="ext_rmrk[]" /></td>
</tr>

<tr>
<td class="notrans">DORD-DK-M-TAM-70</td>
<td>2000 <input value="2000" type="hidden" name="LineNo[]" /> </td>
<td>S160288 <input value="S160288" type="hidden" name="ItemCode[]" /> </td>
<td>SMALL BAG <input value="SMALL BAG" type="hidden" name="ItemName[]" /> </td>
<td> <input value="" type="hidden" name="ExtraRemark[]" /></td>
<td> <input value="" type="hidden" name="ExtraRemark2[]" /></td>
<td>2 <input value="2.00000000000000000000" type="hidden" name="Quantity[]" /> </td>
<td><input required="required" type="text" value="2" name="recv_qty[]" /> </td>
<td> PCS<input type="hidden" value="PCS" name="uom[]" /> </td>
<td><input type="text" value="" name="ext_rmrk[]" /></td>
</tr>

</tbody>
</table>

<a class="pull-right btn btn-warning" id="update" href="#"> Update </a>


and here is my Jquery

$("#update").click(function(){

$this = $(this).val()
$.ajax({
data : { DetTrans : $('.notrans').text() },
url : "<?=base_url();?>dr_mutasi/updateEditItem",
type : "POST",
dataType: 'JSON',
success: function(data) {
console.log(data);
}
})
});


and here is my PHP

function updateEditItem(){
extract(populateform());
echo $DetTrans;
}


So, With my script abobve . I get this from my
console.log


DORD-DK-M-TAM-69DORD-DK-M-TAM-70


As you can see, from my table there are
.notrans
and both of them has different value. Can you tell me how to send via ajax with this format ?

DORD-DK-M-TAM-69;DORD-DK-M-TAM-70


so i can explode it later with my PHP. Thanks in advance.

Answer

$("#update").click(function(){var value="";
                              
  $(document).find('.notrans').each(function(){
    value += (value.length > 0) ? ';' + $(this).text():$(this).text();
  });
  
  $.ajax({
    data : { DetTrans : value},
    url  : "<?=base_url();?>dr_mutasi/updateEditItem",
    type : "POST",
    dataType: 'JSON',
    success: function(data) {
      console.log(data);
    }     
  });
                              
});
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <table class="table table-striped col-md-9 cstm">
    	<thead>
    		<tr><td> <strong>Transaction No</strong> </td> </tr>
    	</thead>
    	<tbody>
    		<tr><td class="notrans">DORD-DK-M-TAM-69</td></tr>
    		<tr><td class="notrans">DORD-DK-M-TAM-70</td></tr>
    		<tr><td class="notrans">DORD-DK-M-TAM-71</td></tr>
    		<tr><td class="notrans">DORD-DK-M-TAM-72</td></tr>
    	</tbody>
    </table>
    <a class="pull-right btn btn-warning" id="update" href="#"> Update </a>

Comments