PHP code inside Jquery, is it possible?

Asked

Viewed 1,669 times

0

I have an application and in it I use Jquery and the function I’m having problems is as follows:

When the user clicks on the button + calls Jquery which reads HTML and PHP content and runs in a row 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);

  • Yes it is possible but it will not work the way you expect. Remember that php runs on the server side (only once per request) and javascript on the client side. The most recommended is to use an ajax to mount these combos if they have different values if they are equal can clone the element.

  • What do you mean? In what way?

1 answer

1


You’re using a file .js that is, only javascript can be executed inside, 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 on this button, send an ajax request to a separate file, so each request PHP will run separately and return the desired HTML.

  • How can I do that?

  • 1

    See some tutorials on the Internet (youtube) has this site from w3schools, which many people say is old, but is good for those who are starting it: https://www.w3schools.com/xml/ajax_intro.asp a better would be the website of Mozilla , MDN https://developer.mozilla.org/en-US/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.