How to execute php code when you press the button?

There is a button
<a href="javascript:void(0);" onclick="ByProducts();" class="btn-corner-black btn-corner-black_active ">add to cart.</a>

Want to it to attach such a code, i.e. it needs to run, if you pressed the button above.
<?
if (CModule::IncludeModule("catalog")) {
 if ($PRODUCT_ID) {
Add2BasketByProductID(
$PRODUCT_ID,
 1, //number of product
false
);
 LocalRedirect("/cart/"); //link to basket
}
}
?>

In JS in General is not strong, tell me how you can implement, preferably on the same page.
June 5th 19 at 21:00
2 answers
June 5th 19 at 21:02
To run php, there is only 2 ways is to open the url with the code, whether click the link , or using $.ajax, $.post, $.get or otherwise it is necessary to tell the server that you want to run php and get the result.
June 5th 19 at 21:04
ByProducts function() {
$.ajax({
 url: url, //path to the php file
 data: data, //data you want to send
 method: 'get' //(or 'post'),
 success: function() {

}
})
}


If you want to pass data, pass data to the function parameter ByProducts

Find more questions by tags JavaScript