1
votes

PHP Function:

    function fill_product($pdo){

$output = '';
    
$select=$pdo->prepare("select * from tbl_product order by pname asc"); 
$select->execute();
    
if ($select->rowCount()) {
        while ($row = $select->fetch(PDO::FETCH_OBJ)) {
            $output .= "<option value='{$row->pid}'>{$row->pname}</option>";
        }
    }
    return $output;
}

I am calling the above PHP function inside below jquery code:

$(document).ready(function() {

 $(document).on('click', '.btnadd', function() {

  var html = '';
  html += '<tr>';

  html += '<td><input type="hidden" class="form-control pname" name="productname[]" readonly></td>';

  html +=
   '<td><select class="form-control productid" name="productid[]" style="width: 250px";><option value="">Select Option</option><?php echo fill_product($pdo); ?> </select></td>';

  html += '<td><input type="text" class="form-control stock" name="stock[]" readonly></td>';
  html += '<td><input type="text" class="form-control price" name="price[]" readonly></td>';
  html += '<td><input type="number" min="1" class="form-control qty" name="qty[]" ></td>';
  html += '<td><input type="text" class="form-control total" name="total[]" readonly></td>';
  html +=
   '<td><center><button type="button" name="remove" class="btn btn-danger btn-sm btnremove"><span class="glyphicon glyphicon-remove"></span></button><center></td></center>';

  $('#producttable').append(html);


  //Initialize Select2 Elements
  $('.productid').select2()

  $(".productid").on('change', function(e) {

   var productid = this.value;
   var tr = $(this).parent().parent();
   $.ajax({

    url: "getproduct.php",
    method: "get",
    data: {
     id: productid
    },
    success: function(data) {

     //console.log(data); 
     tr.find(".pname").val(data["pname"]);
     tr.find(".stock").val(data["pstock"]);
     tr.find(".price").val(data["saleprice"]);
     tr.find(".qty").val(1);
     tr.find(".total").val(tr.find(".qty").val() * tr.find(".price").val());
     calculate(0, 0);
    }
   })
  })

However, is not working. It is related to PHP version change. It was working in 7.3 but not in 8 Can anyone tell me how I can fix this? if I remove from the jquery it works.

What does "not working" mean?maiorano84
it does not populate list of products for the database when I click .btnadd button. Jquery doesn't execute. However, when I remove <?php echo fill_product($pdo); ?> from the jquery, it adds a new table rowuser2383308