我有一个更新表格,我也有预览选项。我的表格如下:
<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>
点击预览后,我使用Jquery获取输入的数据。但是当我在获取图像并将其存储到文件夹中时遇到问题。 我的Jquery如下:
<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>
获取图像文件时收到的所有内容都是[Object-File]。如何使用Jquery将其保存在文件夹中? 我的预览操作如下:
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,
]);
}
}
}
请建议。任何建议都将不胜感激。