I'am trying to make AJAX request and response with images in Yii and I don't know even where to start. I managed to display images from images folder using db requests for file names. I want to make it so when I click on one image it sends requests to the server and it updates my div with another pictures with comes out from db.
What I got is Controller:
$modelCars = EeCarTypes::model()->findAll();
$this->render('index',array(
'model'=>$modelCars,
));
And view:
$uploadUrl = Yii::app()->request->getBaseUrl(true) . '/images/upload/cartypes/';
foreach( $model as $key => $item )
{
$imgName = $model[$key]->attributes['car_type'];
$imgID = $model[$key]->attributes['id'];
$imgFullUrl = $uploadUrl . $imgName;
echo "<a href=\"$imgID\"><img src=\"$imgFullUrl\" alt=\"$imgName\"></a>";
}
I tried to find some widget that would do it for me but I coudnt find one. I would appriciate some tips, links, ideas. Thanks you guys a lot.
Index view:
<script>
$('.image').click(function (e) {
$.ajax({
dataType: "html",
data: {primaryKey: "keyImage"}
success: function (data) {
$('#imageContainer').html(data);
}
});
});
</script>
Controller:
function actionIndex()
{
if (Yii::app()->request->isAjaxRequest && Yii::app()->request->getQuery('primaryKey')) {
$model = EeCarTypes::model()->findByPk($_GET['primaryKey']);
echo $this->renderPartial('/view/..', ['model' => $model]);
Yii::app()->end();
}
$this->render('index');
}
View for renderPartial: get $model and print image