Jump to content

Var Id = $(This).attr("id"); Question


zephral

Recommended Posts

HI, so i am pretty new to this, i am printing out a table from a database. and then i want to delete rows from it by clicking on a image. but i am having trouble getting the id passed to the php pages so i have while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['pallet'] . "</td>"; echo "<td>" . $row['content'] . "</td>"; echo "<td>" . $row['box'] . "</td>"; echo "<td>" . $row['loc'] . "</td>"; echo "<td>" . $row['shipment'] . "</td>"; echo "<td>" . $row['category'] . "</td>"; echo "<td>" . $row['track_per_box'] . "</td>"; echo "<td>" . $row['lan'] . "</td>"; echo "<td>" . $row['id'] . "<img class='delete' src='images/trash_can.jpg' /></td>"; echo "</tr>"; } The $row['id'] is the variable i am trying to pass to the php page $(function() {$(".delete").click(function() {$('#load').fadeIn(); var commentContainer = $(this).parent(); var id = $(this).attr("id"); alert(id); $.ajax({ type: "POST", url: "remove_data.php", data: id, cache: false, success: function(response){$('#msg').html(response).fadeIn('fast'); so there is the code so i am getting Undefined index: id from php page, so i need a way to get the $row['id'] to it and i thought something like $(this).attr('id') might work ? any idea how i would get the id in the ajax call?

Link to comment
Share on other sites

You are close. what you would want to do is give the <img> and id attribute and assign the value of $row['id'] to the id. Also, it is invalid to use a number as an ID, so we will have to create a slight workaround for that too. PHP

while($row = mysql_fetch_array($result)){  echo "<tr>";  echo "<td>" . $row['pallet'] . "</td>";  echo "<td>" . $row['content'] . "</td>";  echo "<td>" . $row['box'] . "</td>";  echo "<td>" . $row['loc'] . "</td>";  echo "<td>" . $row['shipment'] . "</td>";  echo "<td>" . $row['category'] . "</td>";  echo "<td>" . $row['track_per_box'] . "</td>";  echo "<td>" . $row['lan'] . "</td>";  echo "<td><img class='delete' id='value_" . $row['id'] . "' src='images/trash_can.jpg' /></td>";  echo "</tr>";}

so now we have the ID as part of the img element, and retrievable with some JS. And the JS

$(function() {  $(".delete").click(function() {	$('#load').fadeIn();	var commentContainer = $(this).parent(); 	var pieces = $(this).attr("id").split("_");    var id = pieces[1]; 	alert(id); 	$.ajax({	  type: "POST",	  url: "remove_data.php",	  data: id,	  cache: false,	  success: function(response){		$('#msg').html(response).fadeIn('fast');	 }  });};

we split the ID to get just the number, and that's how we get the value.http://www.w3schools...jsref_split.asp

Link to comment
Share on other sites

You are close. what you would want to do is give the <img> and id attribute and assign the value of $row['id'] to the id. Also, it is invalid to use a number as an ID, so we will have to create a slight workaround for that too. PHP
while($row = mysql_fetch_array($result)){  echo "<tr>";  echo "<td>" . $row['pallet'] . "</td>";  echo "<td>" . $row['content'] . "</td>";  echo "<td>" . $row['box'] . "</td>";  echo "<td>" . $row['loc'] . "</td>";  echo "<td>" . $row['shipment'] . "</td>";  echo "<td>" . $row['category'] . "</td>";  echo "<td>" . $row['track_per_box'] . "</td>";  echo "<td>" . $row['lan'] . "</td>";  echo "<td><img class='delete' id='value_" . $row['id'] . "' src='images/trash_can.jpg' /></td>";  echo "</tr>";}

so now we have the ID as part of the img element, and retrievable with some JS. And the JS

$(function() {  $(".delete").click(function() {	$('#load').fadeIn();	var commentContainer = $(this).parent(); 	var pieces = $(this).attr("id").split("_");	var id = pieces[1];	alert(id); 	$.ajax({	  type: "POST",	  url: "remove_data.php",	  data: id,	  cache: false,	  success: function(response){		$('#msg').html(response).fadeIn('fast');	 }  });};

we split the ID to get just the number, and that's how we get the value.http://www.w3schools...jsref_split.asp

Thank you so much such a BIG help works now!!!
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...