Php YII,视图中的表单页面,AJAX更新
我有一个CListView,在其中我添加了一个表单来上传图像。这样,每个项目都有自己的文件上载 表单有一个发布请求的ajaxLink。我在accessRule中添加了“foto”。请求已到达我的控制器,没有错误。仅文件未发布,位置未保存在我的数据库中 我认为它无法获取上传文件的实例“image”,因为视图中的表单。我说得对吗?我怎样才能解决这个问题 CListView\u视图:Php YII,视图中的表单页面,AJAX更新,php,ajax,yii,Php,Ajax,Yii,我有一个CListView,在其中我添加了一个表单来上传图像。这样,每个项目都有自己的文件上载 表单有一个发布请求的ajaxLink。我在accessRule中添加了“foto”。请求已到达我的控制器,没有错误。仅文件未发布,位置未保存在我的数据库中 我认为它无法获取上传文件的实例“image”,因为视图中的表单。我说得对吗?我怎样才能解决这个问题 CListView\u视图: <div class="view"> <!--more content and..-->
<div class="view">
<!--more content and..-->
<?php
$model=new Nieuws;
echo $this->renderPartial('_plaatje', array('model'=>$model, 'itemId'=>$data->id)); ?>
</div>
控制器:
public function actionFoto($id)
{
$model=$this->loadModel($id);
if(isset($_POST['Foto']))
{
$model->foto=CUploadedFile::getInstance($model,'image');
if($model->save())
{
$model->foto->saveAs('uploads/');
}
}
}
您的控制器的名称是什么?您的url管理器中有任何规则吗?您的ajax应该指向/到达它。控制器是NieuwsController。我的url管理器中没有规则。当我监视ajax请求时,它会转到:index.php?r=nieuws/foto&id=22,状态为200 ok。如果您的控制器是
nieuws
,并且您的操作是foto
,那么ajax请求应该指向/nieuws/foto
。嗯,好的,我的ajax请求都不再工作了,我在模型中发现了一个错误。因此,我将更新上述内容,使所有内容都可以再次理解。它现在到达我的操作,但它似乎无法使我的文件上载实例。我不知道此规则array('image','length','max'=>255','on'=>foto')
。你确定这样行吗?因为您没有收到任何php错误,所以可能是您的规则之一不匹配?也许你在他们身上贴上一条信息,看看什么被炒了。
class Nieuws extends CActiveRecord
{
public $image;
/**
* @return string the associated database table name
*/
public function tableName()
{
return 'nieuws';
}
/**
* @return array validation rules for model attributes.
*/
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('titel, tekst, verwijderd', 'numerical', 'integerOnly'=>true),
array('foto', 'length', 'max'=>100),
array('datum, verwijderDatum', 'safe'),
array('image', 'file', 'types'=>'jpg, gif, png', 'allowEmpty'=>true, 'on'=>'foto'),
array('image', 'length', 'max'=>255, 'on'=>'foto'),
// The following rule is used by search().
// @todo Please remove those attributes that should not be searched.
array('id, titel, datum, tekst, foto, verwijderd, verwijderDatum', 'safe', 'on'=>'search'),
);
}
}
public function actionFoto($id)
{
$model=$this->loadModel($id);
if(isset($_POST['Foto']))
{
$model->foto=CUploadedFile::getInstance($model,'image');
if($model->save())
{
$model->foto->saveAs('uploads/');
}
}
}