zephral Posted November 30, 2011 Share Posted November 30, 2011 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 More sharing options...
Ingolme Posted November 30, 2011 Share Posted November 30, 2011 AJAX has nothing to do with PHP. The reason $row['id'] doesn't exist is because you didn't select a field labeled "id" from the database. Check your SQL query. Link to comment Share on other sites More sharing options...
zephral Posted November 30, 2011 Author Share Posted November 30, 2011 $row['id'] does exist and is printed out in the table on my page. i want the id that is printed out to by passed to the function $(function() {$(".delete").click(function() {$('#load').fadeIn();var id = $(this).attr("id");alert(id); Link to comment Share on other sites More sharing options...
thescientist Posted November 30, 2011 Share Posted November 30, 2011 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 More sharing options...
zephral Posted November 30, 2011 Author Share Posted November 30, 2011 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. PHPwhile($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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.