PHP code inside of JQuery, is it possible? [duplicate]

0

I have an application and in it I use JQuery and the function that I'm having problems is the following:

When the user clicks the + button, it calls JQuery that reads HTML and PHP content and executes on a line below to insert a new record, but with PHP it does not want to work. How do I?

(function($) {

  RemoveTableRow = function(handler) {
    var tr = $(handler).closest('tr');

    tr.fadeOut(400, function() {
      tr.remove();
    });

    return false;
  };

  var counter = 1;
  jQuery('a.add').click(function(event) {
    event.preventDefault();
    counter++;
    var newRow = jQuery(
      '<tr>' +
      '<td style="width: 300px">' +
      '<div class="form-group">' +
      '<select name="Produto" id="produto" class="form-control select2" style="width: 100%;">' +
      <?php
          $sql = "SELECT id_Produto, cod_Produto, dsc_Produto from produtos ORDER BY cod_Produto ASC";
          $resultado = mysql_query($sql);

          while ($linha = mysql_fetch_array($resultado)) {
              $id_Produto = $linha['id_Produto'];
              $cod_Produto = $linha['cod_Produto'];
              $dsc_Produto = $linha['dsc_Produto'];
                                                    
              echo"<option value='$id_Produto'> $cod_Produto  |  $dsc_Produto   </option>";
          }
      ?>
      '</select>' +
      '</div> ' +
      '</td>' +
      '<td id="un"></td>' +
      '<td id="vol"><input type="text" class="form-control" name="volume" id="volume" style="width: 50px; margin: 0;"></td>' +
      '<td id="qtd"><input type="text" class="form-control" name="quantidade" id="quantidade" style="width: 60px; margin: 0"></td>' +
      '<td id="peso"></td>' +
      '<td id="uni"></td>' +
      '<td id="tot"></td>' +
      '<td id="desc"></td>' +
      '<td id="liq"></td>' +
      '<td></td>' +
      '</tr>'
    );
    jQuery('table.table-bordered').append(newRow);
  });

})(jQuery);
    
asked by anonymous 30.01.2018 / 17:06

1 answer

1

You are using a .js file ie only javascript can be run in there, if you want to run PHP and javascript, you would have to do something like:

//arquivo script.php

<script>
    ...código jquery/javascript
</script>

<?php
foreach ($usuarios as $bla) {
    echo $bla;
} //só um exemplo
?>

But here PHP would run first and could not run again. Solution: Use AJAX Requests. Each time the user clicks this button, send an ajax request to a separate file, so each request PHP will run separately and return the desired HTML.

    
30.01.2018 / 17:14