Home php How to write PHP code in a javascript file correctly

How to write PHP code in a javascript file correctly

Author

Date

Category

Please tell me how to correctly write PHP code into a javascript file.
Here is the honor of the javascript code:

var tickerContainer = $ (document.createElement ('div')). addClass ('ticker-wrapper')
    .append ($ (document.createElement ('div')). addClass ('quantityTicker')
    .append ('& lt; input type = "text" name = "qty" id = "quantity_wanted_' + idProduct + '" class = "text ticker-input" value = "1" & gt;')
    .append ('& lt; a href = "#" data-field-qty = "qty" class = "btn btn-default button-minus product_quantity_down ticker-button ticker-down" & gt; & lt; span & gt; & lt; i class = "icon-minus" & gt; & lt; / i & gt; & lt; / span & gt; & lt; / a & gt; ')
    .append ('& lt; a href = "#" data-field-qty = "qty" class = "btn btn-default button-plus product_quantity_up ticker-button ticker-up" & gt; & lt; span & gt; & lt; i class = "icon-plus" & gt; & lt; / i & gt; & lt; / span & gt; & lt; / a & gt; ')
  );

You need to add this PHP code to value = “1” instead of “1”:

{if isset ($ quantityBackup)} {$ quantityBackup | intval} {else} {if $ product- & gt; minimal_quantity & gt ; 1} {$ product- & gt; minimal_quantity} {else} 1 {/ if} {/ if}

Tell me how to do it correctly.
Thank you!

here is the entire script file (prestahop module ):

$ (document) .ready (function () {
var collection = $ (tickerSelector);
if (collection.length & gt; 0 & amp; & amp; typeof (window.ajaxCart) == 'object') {
  ajaxCart.overrideButtonsInThePage = function () {
    $ (document) .off ('click', '.ajax_add_to_cart_button'). on ('click', '.ajax_add_to_cart_button', function (e) {
    e.preventDefault ();
    var idProduct = parseInt ($ (this) .data ('id-product'));
    var minimalQuantity = parseInt ($ (this) .data ('minimal_quantity'));
    if (! minimalQuantity)
      minimalQuantity = 1;
    if ($ (this) .prop ('disabled')! = 'disabled')
      ajaxCart.add (idProduct, null, false, this, minimalQuantity);
  });
  // for product page 'add' button ...
  $ (document) .off ('click', '#add_to_cart button'). on ('click', '#add_to_cart button', function (e) {
    e.preventDefault ();
    ajaxCart.add ($ ('# product_page_product_id'). val (), $ ('# idCombination'). val (), true, null, $ ('# quantity_wanted'). attr ('value'), null);
  });
    $ ('# cart_block_list .ajax_cart_block_remove_link'). unbind ('click'). click (function () {
      var customizationId = 0;
      var productId = 0;
      var productAttributeId = 0;
      if ($ ($ (this) .parent (). parent ()). attr ('name') == 'customization') {
        var customizableProductDiv = $ ($ (this) .parent (). parent ()). find ("div [id ^ = deleteCustomizableProduct_]");
      }
      else {
        var customizableProductDiv = $ ($ (this) .parent ()). find ("div [id ^ = deleteCustomizableProduct_]");
      }
      if (customizableProductDiv & amp; & amp; $ (customizableProductDiv) .length) {
        $ (customizableProductDiv) .each (function () {
          var ids = $ (this) .attr ('id'). split ('_');
          if (typeof (ids [1])! = 'undefined') {
            customizationId = parseInt (ids [1]);
            productId = parseInt (ids [2]);
            if (typeof (ids [3])! = 'undefined') {
              productAttributeId = parseInt (ids [3]);
            }
            return false;
          }
        });
      }
      if (! customizationId) {
        var firstCut = $ (this) .parent (). parent (). attr ('id'). replace ('cart_block_product_', '');
        firstCut = firstCut.replace ('deleteCustomizableProduct_', '');
        ids = firstCut.split ('_');
        productId = parseInt (ids [0]);
        if (typeof (ids [1])! = 'undefined') {
          productAttributeId = parseInt (ids [1]);
        }
      }
      ajaxCart.remove (productId, productAttributeId, customizationId);
      return false;
    });
  }; 
$ (Document) .OFF ('click', '.ajax_add_to_cart_button'). ON ('Click', '.ajax_add_to_cart_button', function (E) {
  var iDProduct = $ (this) .data ('id-product');
  ajaxcart.add (IDPRODUCT, NULL, FALSE, THIS, PARSEINT ('# QUANTITY_WANTED_' + IDPRODUCT) .ATTR ('Value')));
  RETURN FALSE;
});
$ (Document) .on ('Click', '.ticker-Button', Function (E) {
  E.PreventDefault ();
  var parentdiv = $ (this) .Parents ('Div.QuantityTicker: First');
  if (parentdiv.length) {
    var thisinput = parentdiv.find ('input.ticker-input'),
    thisval = Parseint ('Value'));
    if (ISNAN (THISVAL)) {
      thisval = 1;
    }
    If ($ (this) .is ('. Ticker-Up')) {
      THISVAL ++;
    }
    ELSE IF (THISVAL & GT; 1) {
      thisval--;
    }
    Thisinput.attr ('Value', Thisval);
  }
});
collection.each (function () {
  var iDProduct = $ (this) .data ('id-product');
  VAR TickerContainer = $ (document.createElement ('Div')). AddClass ('Ticker-Wrapper')
    .APPEND (Div.CreateElement ('Div')). AddClass ('quantityTICKER')
    .APPEND ('& lt; Input type = "text" name = "qty" id = "quantity_wanted_' + idproduct + '" class = "text ticker-input" value = "1" & gt;')
    .APPEND ('& lt; a href = "#" data-field-qty = "qty" class = "btn btn-default button-minus product_quantity_down ticker-button ticker-down" & gt; & lt; span & gt; & lt; i class = "icon-minus" & gt; & lt; / i & gt; & lt; / span & gt; & lt; / a & gt; ')
    .append ('& lt; a href = "#" data-field-qty = "qty" class = "btn btn-default button-plus product_quantity_up ticker-button ticker-up" & gt; & lt; span & gt; & lt; i class = "icon-plus" & gt; & lt; / i & gt; & lt; / span & gt; & lt; / a & gt; ')
  );
  TickerContainer.insertBefore ($ (this));
  var currentinput = tickercontainer.find ('div.ticker-input-wrapper'),
  currentInputHeight = currentinput.outerheight ();
    //tickercontainer.find('div.ticker-button').CSS( ('Height ': CurrentInputHeight +' px '});
  });
}
});

Answer 1, Authority 100%

Write, of course, it is possible, but it is probably meaningless.

After all, most likely javascript -code in your case runs js -inreader in client browser.

But, firstly, the client’s browser is unlikely to execute the code written for the php interpreter, and secondly, even if (suddenly) and will, he (browser) is not exactly To those used on your site variables, classes and everything else.


Your question acquires meaning only if the javascript is formed by the interpreter php inside your site.

Then, for example, you can use the “ordinary” PHP Stand:

HTML text (including JS code) & lt;? php here php code? & gt; More HTML text

After processing such a file with an interpreter php instead of & lt;? php ...? & gt; will be substituted Result works php -Code.


Answer 2, Authority 100%

If you assume that the JS from the first block is in the template, the PHP code (which is generally not PHP, and the code for the template in PrestaShop) can be delivered so:

.append ('& lt; input type = "text" name = "qty" id = "quantity_wanted_' + iDProduct + ' "Class =" Text Ticker-Input "Value =" {if isset ($ quantitybackup)} {$ quantitybackup | intval} {else} {IF $ Product- & gt; minimal_quantity & gt; 1} {$ product- & gt; minimal_quantity} { ELSE} 1 {/ if} {/ if} "& gt; ')

Answer 3, Authority 100%

Thank you all! I found the answer myself turned out to be simple
Added a variable

var minimalquantity = parseint ($ (this) .data ('minimal_quantity'));

and brought it out of Value = “‘+ MinimalQuantity +'”

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions