javascript - Ajax post data while loop php -
sorry, beginner. have problem ajax post data while loop php. here code.
assume after while loop have 5 records 5 buttons different post data each record, ajax send first data of record no matter click button in these 5 records ajax send same data of first record data
but, if use $(this).val() ajax send right data in each records.
pls help. thx much.
in php.
<?php while($rs = mysql_fetch_array($qr)) { ?> <td width="150"> <input type="text" name="studentid" class="studentid" value="<?=$rs['studentid']?>"/> </td> <td width="100"> <select name="ssize" class="ssize"> <option value="">ไซต์</option> <option <?php if($rs['ssize'] == 's') { ?> selected="selected" <?php } ?> value="s">s</option> <option <?php if($rs['ssize'] == 'm') { ?> selected="selected" <?php } ?> value="m">m</option> </select> </td> <button value="<?=$rs['id']?>" class="printbill btn btn-primary"> <i class="glyphicon glyphicon-duplicate"> </i> พิมพ์ใบเสร็จ </button> <?php } ?>
in js.
$(".printbill").click(function () { $.ajax({ url: "admin_search_save.php", data: { id:$(this).val(), studentid:$(".studentid").val(), ssize:$(".ssize").val(), }, success: function (data) { } }); });
you need use event delegation dynamically added content.
$(document).on('click','.printbill',function () { // capture input , select value traversing parent(tr), // find element [input, select] // traversing method depend on table structure var studid = $(this).closest('tr').find('input[name=studentid]').val(), ssizedata = $(this).closest('tr').find('select[name=ssize]').val(); $.ajax({ url: "admin_search_save.php", data: { id : $(this).val(), studentid : studid, ssize : ssizedata, }, success: function (data) { } }); });
Comments
Post a Comment