Thursday 5 December 2019

javascript - Checkbox in a dynamic table with dynamic input fields



I have a table with dynamic values and hidden input fields and dynamic checkbox. Checkboxes have same class names. When i click on a particular checkbox i want the input values from that particluar row should be sent to ajax.



Below is the code for dynamic table,















$owner=mysql_query("select id,tmp_id,name,phone,activate from pgowner where active='1'");
if(mysql_num_rows($owner)){
$i=0;
while($owner1=mysql_fetch_array($owner)){
$id=$owner1['tmp_id'];
?>







?>

Sl.noOwner NameContact NumberAction











Here's the ajax code to fetch the value from the row in which checkbox is checked.






I am not able to fetch the input value from the row in which checkbox is checked. Please help.



Generated HTML looks like this,






























Sl.noOwner NameContact NumberAction
1
Name1


9731269342


2
Name2


7760807087





Answer



There are multiple issues.



Since you have the controls in a loop, using ID will create multiple elements with same ID which is invalid, instead use class or other selectors like attribute selector to select elements.







then use a class selector to register the click hanlder and then find the owner id in the same row as given below




$(document).ready(function() {
$('.activate').click(function() {
var ownerid = $(this).closest('tr').find('input[name="ownerid"]').val();
var a = this.checked ? 1 : 0;

$.ajax({
type: "POST",
url: "profileactivation",
data: {

value: a,
ownerid: ownerid
},
success: function(html) {
alert("Successfully Done");
}
});
});
});


No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...