Good day. There is a table with multiple entries. each row of this table has a drop-down list for selecting the parameter we need and a button. When you click on the button, the data gets into the ajax handler. The problem is that in ajax only the very first entry from the drop-down list is sent, and it does not matter which button to press. Here's the php code:

foreach ($sql as $data){ echo '<th><select>'; foreach ($sql2 as $data2){ echo '<option id="ProdID" value="'.$data2["ProdID"].'">'.$data2["ProdName"].'</option>'; } echo'</select></th>'; echo '<th><input type="button" data-old="'.$data['OLDID'].'"data-id="'.$data['id'].'" value="ok" class="button"/></th>'; } 

Here is my ajax handler:

 jQuery(document).ready(function(){ $('.button').click(function(event) { event.preventDefault(); var id=$(this).data("id"); var oldid=$(this).data("old"); var ProdID=$("#ProdID").val();//Данные из выпадающего списка jQuery.post('insert.php, { id:id , oldid:oldid , ProdID:ProdID}, function(data) { alert(ProdID); } ); }); }); 

Yes, I know that I need to do this through the class, but I do not know how to pass the parameter. I know more precisely, but I only know the transfer of parameters from the button by pressing it.

  • Ps. The data is transmitted, but the very first value of ProdID falls into the variable - user209681

1 answer 1

the variable ProdID gets the value of the first found element with the ID #ProdID

this should help

 foreach ($sql as $data){ echo '<th><select id="ProdID_'.$data['id'].'">'; foreach ($sql2 as $data2){ echo '<option value="'.$data2["ProdID"].'">'.$data2["ProdName"].'</option>'; } echo'</select></th>'; echo '<th><input type="button" data-old="'.$data['OLDID'].'"data-id="'.$data['id'].'" value="ok" class="button"/></th>'; } jQuery(document).ready(function(){ $('.button').click(function(event) { event.preventDefault(); var id=$(this).data("id"); var oldid=$(this).data("old"); var ProdID=$("#ProdID_"+$(this).data('id')).val();//Данные из выпадающего списка jQuery.post('insert.php, { id:id , oldid:oldid , ProdID:ProdID}, function(data) { alert(ProdID); } ); }); }); 
  • now the data from the first value of the drop-down list is transferred, that is, if there are values ​​(1,2,3) in the drop-down list, it will transmit only 1. - user209681
  • @ user209681 did not notice. The ID should be assigned to the SELECT element, not OPTION - webDev_
  • Yes it works. Thank you very much, and I ajax full zero ( - user209681
  • @ user209681 don't forget to accept the answer - webDev_