Raghib Ahsan Raghib Ahsan - 7 months ago 21
PHP Question

Selecting a unique cell from a dynamic generated table

Here goes my problem: this is the table

enter image description here

I want to click on delete column of a particular row say row no2 then I should select the value

OFFICED-Id
of that particular row row no.2

The problem is that table is being generated dynamically by the following code

<tbody>
<?php for($i=0;$i<count($detail1);$i++):?>
<tr>
<td><?=$detail1[$i]['id'];?></td>
<td><?=$detail1[$i]['officer_id'];?></td>
<td><button onclick='alert("<?=$detail1[$i]['password']?>")' > change</button></td>
<td><i class=" del glyphicon glyphicon-minus"></i></td>
</tr>
<?php endfor; ?>
</tbody>


I am trying to select that particular cell of a row by applying this jQuery function but I am getting error in the console and it's not working too

the variable
id
is going to save that particular cell value

$(document).ready(function(){
$(".del").click(function(){
var answer = confirm ("Are you sure you want to delete from the database?");
var id= <?php echo $detail1[$i]['id'];?>;


if (answer)
{
// your ajax code
$.ajax({
type: 'POST',
url: '<?php echo base_url(); ?>Admin/del', //We are going to make the request to the method "list_dropdown" in the match controller
data: {'id':id}, //POST parameter to be sent with the tournament id
//With the ".html()" method we include the html code returned by AJAX into the matches list
success: function(resp) {
alert('you have successfully deleted');
},
error: function(resp) {
console.log('error');
console.log(arguments);
}
});
}
});
});


The error in the console is


SyntaxError: expected expression, got '<'




What I figured out that this line var
id= <?php echo $detail1[$i]['id'];?>
is causing error and making my jQuery call not functioning

Is there alternative to select that particular cell OFFICED OF A ROW from a dynamic table by any event?

Answer
var id= <?php echo $detail1[$i]['id'];?>

Looks like $i is not defined, so id should be null. And so the issue.

Select id something like this. Please test, might need extra work:

var id= $(this).closest('tr').find('td.id').html();

And HTML <td> of id line should look something like:

<td class="id"><?=$detail1[$i]['id'];?></td>
Comments