Answer the question
In order to leave comments, you need to log in
How to get button id correctly?
My js script works a little strange:
<?php
use app\models\User;
use yii\helpers\Html;
use app\components\PeopleWidget;
use yii\helpers\Url;
/**
* @var User $peopleArr
* @var PeopleWidget $context
*/
?>
<?php foreach ($peopleArr as $people): ?>
<?php echo Html::a($people->getAttribute('name'), Url::to(['/user/index', 'id' => $people->getAttribute('nick_name')]));?>
<?php echo $people->getAttribute('nick_name');?>
<?php echo Html::button('Сообщение', ['id' => $people->getId(), 'class' => 'mail-to'])?>
<?php echo Html::button($context->isFriend($people->getId()) ? PeopleWidget::REMOVE_FROM_FRIEND : PeopleWidget::ADD_TO_FRIEND,
['id' => PeopleWidget::REMOVE_FROM_FRIEND ? $context->getFriendId($people->getId()) : $people->getId(), 'class' => 'action-to-user']);?>
<?php endforeach;?>
<?php
$removeText = PeopleWidget::REMOVE_FROM_FRIEND;
$addText = PeopleWidget::ADD_TO_FRIEND;
$checkFriend = <<< JS
var removeText = "$removeText";
var addText = "$addText";
$('.action-to-user').click(function() {
if ($(this).text() == removeText) {
$.ajax('/user/delete-friend/', {
type: 'post',
data: 'id-friend=' + this.id + '&is-people=' + true,
success: function (response) {
$(('#'+response)).text(addText);
},
error: function() {
alert('error');
}
});
}
if ($(this).text() == addText) {
$.ajax('/user/add-friend/', {
type: 'post',
data: 'id-friend=' + this.id,
success: function(response) {
alert(response);
},
error: function() {
alert('error');
}
});
}
});
JS;
$this->registerJs($checkFriend, \yii\web\View::POS_READY);
?>
id
button is passed correctly and the action is performed correctly! action
an empty string is passed to the controller! id
, for example 5 - the result does not change! if ($(this).text() == addText) {
$.ajax('/user/add-friend/', {
type: 'post',
data: 'id-friend=' + 5,
success: function(response) {
alert(response);
},
error: function() {
alert('error');
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question