Sam Hester Sam Hester - 3 months ago 7
Ajax Question

Seperating <td> in a <tr> using jquery when pulled from an onclick event

Before we start i am fairly new to jquery and do not know most of the functions and capability, so i do not know if there is an easy trick to do this that i am missing.

I am having an issue where I am using a search function to display a table of results using Jquery, ajax and PHP. Once the results table is displayed form the search, I have a clickable link on the final row of the table. This link should get the data from the row you clicked on and set it to a

$_SESSION
in PHP. I can get the correct data fine, I just cannot separate out each
<td>
field individually. I wish to do this by adding in a comma or a symbol that I could use to identify each separate field once in PHP. This is an issue as I wish to put it into an array to auto-fill some input on the next page that you get redirected too. Can anyone help or point me in the right direction for this.

Thanks

Jquery + ajax

$('#resultviews').on("click",".buttonaslink",function(){
var tabler = $(this).closest("tr").text();
tabler = ('tabler=' + tabler);
alert(tabler);

$.ajax({
type: "POST",
url: "rowsession.php",
data: tabler,
cache: false,
success: function(html){
$('#resultviews').html(html).show();
}});
return false;
});


PHP how results are displayed in the table from first ajax POST

while ($row = mysqli_fetch_array($view)){
$id = $row['companyId'];
$company = $customers[$id];
$company = ucwords($company);

echo "<tr><td class='tabled'>". $row['ip']."</td><td class='table'>". $row['Subnet']."</td><td class='table'>".$row['hostName']."</td><td class='table'>". $row['owner']."</td><td class='table'>". $company ."</td><td class='table'>".$row['siteName']."</td><td class='table'><button id='rowbtn' class='buttonaslink'>Edit</button> / <button id='rowdel' class='btnaslink'>del</button><tr>";
}

Answer

You can try this approach:

var tabler = $(this).closest("tr").find("td")
  .map(function() {
    return $(this).text();
  })
  .get()
  .join(",");

It takes a value from every td element of the row and joins them with a comma. If you need to remove a trailing comma, you can use this code:

if(tabler.length) {
    tabler = tabler.substring(0, tabler.length - 1);
}