I have an update form where i also have preview option. My form is as below:
<div class="whyyour-form">
<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); ?>
<?= $form->field($model, 'heading')->textInput([]) ?>
<?= $form->field($model, 'image')->fileInput();?>
<div class="myimage"><?php if(isset($model->image) && !$model->isNewRecord) {
echo Html::img(\yii\helpers\Url::to($baseUrl.'/uploads/'.$model->image),['class' => 'image','width'=>200]);}?>
</div>
<?= $form->field($model, 'content')->textarea(['rows' => 10]) ?>
<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? 'Save' : 'Save', ['class' => $model->isNewRecord ? 'btn mybtn' : 'btn mybtn']) ?>
<?= Html::button('Preview',['id' => 'preview','class' => 'btn mybtn']); ?>
</div>
<?php ActiveForm::end(); ?>
</div>
On click of preview, I'm using Jquery to get the entered data. But when I have problem in getting the image and storing it into folder. My Jquery is as below:
<script>
$( document ).ready(function() {
$('#w0').on("click", function(event) {
$form = $(this); //wrap this in jQuery
id = $form.attr('action');
lastChar = id.substr(id.length - 1);
});
$('#preview').click(function() {
var img =$("#whyyour-image")[0].files[0]; alert(img);
var tagln = $("#whyyour-heading").val();
tinyMCE.triggerSave();
var des =$('#whyyour-content').val();alert(des);
$.ajax({
method: "POST",
url:"<?php echo Yii::$app->urlManager->createAbsoluteUrl(['whyyour/preview'])?>",
data: { id : lastChar, tag : tagln , description : des },
success:function(data) {
}
});
setTimeout(function() {
validateForm();
},1000);
});
function validateForm() {
var url = 'http://localhost/yourcms/your/coststructure.php?id='+lastChar;
window.open(url);
};
});
</script>
All I receive when I get the image file is [Object-File]. how to save it in the folder using Jquery? My preview action is as below:
public function actionPreview()
{
$model = $this->findModel($_REQUEST['id']);
if($model){
if(isset($_REQUEST['tag']) && !empty($_REQUEST['tag']) && isset($_REQUEST['description']) && !empty($_REQUEST['description']) ){
$model->previewHeading= $_REQUEST['tag'];
$model->previewContent = $_REQUEST['description'];
if($model->save()) {
\Yii::$app->getSession()->setFlash('error', 'Product(s) Removed Successfully');
} else {
return $this->render('update', [
'model' => $model,
]);
}
} else {
return $this->render('update', [
'model' => $model,
]);
}
}
}
Please Suggest. Any suggestions would be greatly appreciated.