Created two TVs greenButton and redButton - numbers, when you click on the button to increase them by +1
just something does not work, I understand the ajax script but I do not understand why
<button class="btn green" data-btntype="greenButton">like [[!+modx.user.id:memberof=`Administrator`:then=`([[*greenButton]])`:else=``]] </button> <button class="btn red" data-btntype="redButton">dlike [[!+modx.user.id:memberof=`Administrator`:then=`([[*redButton]])`:else=``]] </button> <script type="text/javascript"> $(document).on('click', '.btn', function() { $.ajax({ type: 'POST', url: '[[*uri]]', data:'&action=countButton&but=' +$(this).attr('data-btntype'), success: function(msg){ msg = JSON.parse(msg); window.location.href = msg.url; } }); return false; }); </script> snippet ajax
<?php if ($_SERVER['HTTP_X_REQUESTED_WITH'] != 'XMLHttpRequest') {return;} $action = $_POST['action']; if (empty($action)) {return;} $res = ''; echo $_POST['action']; switch ($action) { case 'countButton': $resource = $modx->getObject('modResource', $modx->resource->id); // Получаем предыдущее значение счетчика $count = $resource->getTVValue($_POST['but']) + 1; // Устанавливаем новое значение $resource->setTVValue($_POST['but'],$count); $resource->save(); // Очищаем кеш ресурса, чтобы изменения отобразились сразу $resource->_contextKey = $resource->get('context_key'); $cache = $modx->cacheManager->getCacheProvider( $modx->getOption('cache_resource_key', null, 'resource')); $key = $resource->getCacheKey(); $cache->delete($key, array('deleteTop' => true)); $cache->delete($key); // Возвращаем URL, по которому нужно отправить пользователя $res = $modx->toJSON(array('url' => $_POST['url'])); } if (!empty($res)) { die($res); }