-5

I have this JavaScript code

var list = "";
$.each(data.result.products, function (i, item) {
    var prices = item.salePrice;
    list = list +
        '<h3>' + item.productTitle + '</h3>' +
        '<h4>' +  item.salePrice + ' </h4>' +
        '<?php $price = "" ?>' +
        ;
})

I want item.salePrice in PHP variable <?php $price = "" ?>. What will be the best solution to assign price value to PHP variable.

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Muhammad Riaz
  • 403
  • 3
  • 8
  • 23

1 Answers1

0

You are trying to do something on server from client which is impossible the way you do it. Javascript is run on client side, that means you can´t access server because you don´t have direct access to the server. What allows you to make calls to the server from client is jQuery.ajax() which performs asynchronous call to the server. That way you can make server changes. The way you do it, you just insert some php code which will never be run, because the site is already fully loaded.

beranpa8
  • 435
  • 1
  • 5
  • 11