javascript - How to pass the table cell value via ajax to php -
i want when click table cell value pass php via ajax show value in alert box. wrote code show alert box value [object htmltablecellelement]..
anyone help,
here code:
function found(row) { var table=document.getelementbyid("table"); var rows = table.getelementsbytagname("tr"); (var = 0; < rows.length; i++) { rows[i].onclick = (function() { return function() { var result = this.cells[1]; var plate = this.cells[6]; var km = this.cells[7]; var datastring = 'plate=' + plate + '&km='+km; $.ajax({ type: 'post', url: 'km.php', async: true, data: datastring, cache: false, global: false, success: function(result) { alert("succ : " + result); } }); } })(i); } }
my php:
<?php $plate = $_post['plate']; $km = $_post['km']; echo $plate; echo $km; ?>
you're setting variables table cell elements, not text in them.
var result = this.cells[1].textcontent; var plate = this.cells[6].textcontent; var km = this.cells[7].textcontent;
btw, there doesn't seem need use iife pattern around onclick
function. that's needed if function refers i
, doesn't.
btw, can write whole thing more succinctly using jquery:
$("#table tr").click(function() { var cells = $(this).find('td'); var result = cells.eq(1).text(); // isn't used? var plate = cells.eq(6).text(); var km = cells.eq(7).text(); $.ajax({ type: 'post', url: 'km.php', async: true, data: {plate: plate, km: km}, cache: false, global: false, success: function(result) { alert("succ : " + result); } }); });
Comments
Post a Comment