PHP inside the Javascript If function?
one text
Solution:
You can't use PHP code inside the javascript file like this. For that, you have to use Ajax and Jquery and pass data in Ajax call to PHP file. You can refer Jquery and ajax documentation. Read https://jquery.com and https://api.jquery.com/jquery.ajax/
You have to pass those data to the javascript file by form or data attributes. After that, you can call PHP file using ajax and execute your code what you want.
$current_user = wp_get_current_user();
$current_user_id = $current_user->ID;
$user_coins = get_user_meta( $current_user_id, 'usercoins' , true );
$user_coins_sum_plus = $user_coins + 1;
$user_coins_sum_minus = $user_coins - 1;
update_user_meta( $current_user_id, 'usercoins', $user_coins_sum_plus);
For example, This will works
alert( <?php echo "'Hello'"; ?> );
But you can't perform any operation like this:
function my_function()
{
<?php
$num1 = 2;
$num2 =3;
$sum = $num1 + $num2;
echo $sum;
?>
}
Source