Answer the question
In order to leave comments, you need to log in
Ajax script only changes styles after page reload?
Hello. It is necessary to implement "Selected goods" on the website of the online store on Bitrix.
I found a solution on the Internet that works, but not completely.
The bottom line is this: create a php file with the code:
<?
global $USER;
if (CModule::IncludeModule ( "iblock" )) {
$elId = intval ( $_REQUEST ["elid"] );
$userId = $USER->GetID ();
$dbEl = CIBlockElement::GetList ( Array (), Array ("ID" => $elId, "IBLOCK_ID" => "2" ) );
if ($obEl = $dbEl->GetNextElement () AND !empty($userId)) {
$props = $obEl->GetProperties ();
$UserList = array_unique($props["F_USER"]["VALUE"]);
if(!in_array($userId, $UserList)) {
$UserList[] = $userId;
CIBlockElement::SetPropertyValueCode ($elId, "F_USER", $UserList);
echo "done";
}
else {
$key = array_search($userId, $UserList);
unset($UserList[$key]);
CIBlockElement::SetPropertyValueCode ($elId, "F_USER", $UserList);
echo "deleted";
}
}
else {
echo "fail";
}
}
exit;?>
<?if(in_array($USER->GetId(),$arItem["PROPERTIES"]["F_USER"]["VALUE"])){
$check = " checked";
}
else{
$check = "";
};?>
<a href="#" class="catalog-item-fav favorite_check<?=$check?>" data-id="<?=$arItem['ID']?>"></a>
<div class="favorit_label"> <?=$val?></div>
<script>
$("document").ready(function(){
$(".favorite_check").click(function(e){
e.preventDefault();
var $this = $(this);
$.get("/personal/favoriteajax.php", {
elid: $this.data('id')
}, function(data){
switch(data){
case 'done' : var response = 'Товар успешно добавлен в избранное';$this.addClass("checked");break;
case 'deleted' : var response = 'Товар успешно удален из избранного';$this.removeClass("checked");break;
case 'fail' : var response = 'Вы не авторизованы, либо Ваш запрос некорректен';$this.removeClass("checked");break;
}
$(".favorit_label").html(response).fadeIn().delay(2000).fadeOut();
});
});
});
</script>
Answer the question
In order to leave comments, you need to log in
I solved the issue myself! in the php file did not add the line before the code:
<? require($_SERVER['DOCUMENT_ROOT'].'/bitrix/modules/main/include/prolog_before.php');?>
After adding everything worked!
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question