admin管理员组文章数量:1287535
The idea is to add two values and the total of the sum save it or update it in user_meta with jquery.ajax. This is the code, but it gives me an error in the console Uncaught ReferenceError: 'total' is not defined
Please, Any help is appreciated!
My Button
<a href="" onclick="CounterTrav()" id="btnTraPack"></a>
javascript
<script>
//This sum the tow values i get from a click and works OK!
const clicksContainer = document.querySelector('.clicksContainer');
const totalEl = document.createElement('span');
clicksContainer.appendChild(totalEl);
clicksContainer.addEventListener('click', function(e) {
if (!e.target.classList.contains('clicksContainer')) {
const shareCLick = document.querySelectorAll('.resultTraveler');
let total = 0;
shareCLick.forEach(item => {
total += parseInt(item.innerText, 10)
totalEl.innerHTML = total
})
console.log(total);
}
})
var clickCounterTrav = 0
function CounterTrav() {
clickCounterTrav += 1;
document.getElementById("resultTraveler").innerHTML = clickCounterTrav + " Travel Pack you have Shared!";
console.log(clickCounterTrav);
var CounterTrav_update = 'clickCounterTrav';
jQuery.ajax({
url: '/wp-admin/admin-ajax.php',
data : {
action : 'CounterTrav_update',
CounterTrav_value : CounterTrav_value,
},
beforeSend: function() {
console.log('Updating Field');
},
success : function( response ) {
console.log('Success');
},
});
}
</script>
// MY ISSUE START HERE If I use
var CounterTrav_update = 'clickCounterTrav';
The result of the sum is Correct, and the user_meta is Update OK!, BUT only update "clickCounterTrav" Like Eg: The user onready have in user_meta 2 clicks storage, and in session2 click just one time, then update user_meta with 1 value.
PHP
add_action( 'wp_ajax_CounterTrav_update', 'CounterTrav_update' );
add_action( 'wp_ajax_nopriv_CounterTrav_update', 'CounterTrav_update' ); // This lines it's because we are using AJAX on the FrontEnd.
function CounterTrav_update(){
if(empty($_REQUEST) || !isset($_REQUEST)) {
ajaxStatus('error', 'Nothing to update.');
} else {
try {
$user = wp_get_current_user();
$CounterTrav_value = $_GET['CounterTrav_value'];
update_user_meta( $user->ID, 'clickCounterTrav', $CounterTrav_value );
die();
} catch (Exception $e){
echo 'Caught exception: ', $e->getMessage(), "\n";
}
}
}
If I use
var CounterTrav_update = 'total';
Then the Sum result is Nan and in Console error is: Uncaught ReferenceError: total is not defined
本文标签: javascript onClick update usermeta from jqueryajax
版权声明:本文标题:javascript onClick update user_meta from jquery.ajax 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741312237a2371711.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论