Afaf Afaf - 1 month ago 11
Ajax Question

Ajax with yii2 controller

When I click on my link , I would that when I click on the link i reload another div content and when I past the full link in the navigator It's not accessible.

<a onclick="getConversation('id_div_2','<?= $id;?>','<?= $user1['id'];?>','admin/ajaxMessage','<?= Yii::$app->getUrlManager()->getBaseUrl();?>')"
class="list-group-item">


I got the following error :

enter image description here

That's my controller script

public function actionIndex()
{

if(isset($_POST['idCurrentUser'])){

$idCurrentUser = $_POST['idCurrentUser'];
$idUser = $_POST['idUser'];

$all_conversations = Yii::$app->db->createCommand(" SELECT * FROM message WHERE sender_id=".$idCurrentUser." OR receiver_id=".$idCurrentUser. " AND last_message=True" )->queryAll();

$user1= Yii::$app->db->createCommand(" SELECT * FROM user WHERE id=".$idUser )->queryOne();

$one_conversation = Yii::$app->db->createCommand(" SELECT * FROM message WHERE ( sender_id=".$idCurrentUser." AND receiver_id=".$idUser.") OR ( sender_id=".$idUser." AND receiver_id=".$idCurrentUser.")" )->queryAll();

?>
<div class="panel-heading">
<div class="media">
<a href="mail-compose.html" class="btn btn-danger pull-right rounded"><i class="fa fa-share"></i></a>
<a class="pull-left" href="#">
<img src="http://img.djavaui.com/?create=50x50,144966?f=ffffff" alt="Rebecca Cabean" class="img-circle"/>
</a>
<?php $contact1= Yii::$app->db->createCommand(" SELECT * FROM user WHERE id=".$idCurrentUser)->queryOne(); ?>
<?php $contact2 = Yii::$app->db->createCommand(" SELECT * FROM user WHERE id=".$idUser)->queryOne(); ?>
<?php $one_conversation1 = Yii::$app->db->createCommand(" SELECT * FROM message WHERE ( sender_id=".$idCurrentUser." AND receiver_id=".$idUser.") OR ( sender_id=".$idUser." AND receiver_id=".$idCurrentUser.")" )->queryOne(); ?>
<?php $msg1= $one_conversation1; ?>
<div class="media-body">
<?php if ($msg1['sender_id']==$idCurrentUser ) { ?><h4 class="media-heading"><?= $contact2['username']; ?> <small>(<?= $contact2['fonction']; ?>)</small></h4><?php } ?>
<?php if ($msg1['receiver_id']==$idCurrentUser ) { ?><h4 class="media-heading"><?= $contact2['username']; ?> <small>(<?= $contact2['fonction']; ?>)</small></h4> <?php } ?>
<small>Thursday 5th July 2014-via Intercom</small>
</div>
</div>
</div><!-- /.panel-heading -->
<div class="panel-body">
<?php foreach ($one_conversation as $key => $msg ) : ?>
<p>
<?= $msg['text'] ?>
</p>
<hr/>
<?php endforeach; ?>
</div><!-- /.panel-body -->

<?php
return false;
}

}


And my ajax function looks like

function getConversation(IDdiv,idCurrentUser,idUser,ctrl,url){

var idCurrentUser = idCurrentUser;
var idUser = idUser;

var dataForm = {idCurrentUser: idCurrentUser,idUser: idUser};
$.ajax({
type: 'POST',
url:''+url+'/index.php?r='+ctrl+'/index',
data:dataForm,
success: function (response) {
document.getElementById(IDdiv).innerHTML=response;
}
});
}

Answer

I think the problem in type= 'POST', you try to use onclick in a tag and so you should use GET method in type ajax like that:

function getConversation(IDdiv,idCurrentUser,idUser,ctrl,url){

var CurrentUser = idCurrentUser;
var User = idUser; 

$.ajax({
    type: 'GET',
    url:''+url+'/index.php?r='+ctrl+'/index',
    data:'CurrentUser='+CurrentUser+'&User='+User,
    success: function (response) {
        document.getElementById(IDdiv).innerHTML=response;
    }
  });
}
Comments