So right now I have this JS code:
function scoreCalculation(random, userPick) {
if (userPick === random) {
userScore++;
winner.innerHTML = `<h3><font color="green">You won</font></h3>`;
}
}
Which basically adds +1 to a score and shows in an html.
What I'm trying to achieve is to add that score into the database using update_user_meta
PHP when userPick===random.
I'm trying to execute this PHP (tested and working) inside the javascript code:
<?php $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;
update_user_meta( $current_user_id, 'usercoins', $user_coins_sum_plus);
?>
I have tried this mix, but it doesn't work. Basically JS code stops working at all:
function scoreCalculation(random, userPick) {
if (userPick === random) {
userScore++;
<?php $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);
?>;
winner.innerHTML = `<h3><font color="green">You won</font></h3>`;
}
I guess I'm making a slight mistake adding the php inside the javascript?
Any help would be appreciated.