'How to pass JavaScript variables to PHP without reload the page?

i want to assign javascript variable value to php variable to loop products

here is my php code:

$products = wc_get_products( array( 
  'include' => $products_ids // `**i want to pass value from javascript here**`
) );

  foreach ( $products as $product ) {
     // fetching my product details
  }

here is my js code:

  (function($){

    $(document).ready(function(){

      $(document).on('change', '#myform', function(e) {

        
        e.preventDefault();

        data = $(this).serialize();

        var settings = {
            "url": "<?php  echo WC_AJAX::get_endpoint( 'myajaxfunction' ) ?>",
            "method": "POST",
            "data": data,
          }
          $.ajax(settings).done(function (result) {
            // i want to make $products_ids = result
            // result value is array(1,2);
          });
      });
    });
  })(jQuery);

**

  1. i want to make $products_ids = result so i can pass it in my php code,

  2. result value is array(1,2);

**



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source