【问题标题】:Yii2 Submiting form starts downloading view fileYii2 提交表单开始下载视图文件
【发布时间】:2019-08-05 13:22:40
【问题描述】:

我在名为bestellungadmin.php 的页面上有一个简单的yii2 表单 如果我点击提交按钮,数据没有提交,它开始下载一个名为bestellungadmin的文件

浏览器控制台打印消息:

jquery.js?v=1556869380:7841 Resource interpreted as Document but transferred with MIME type application/x-httpd-php: "https://rueckschlag.com/bestellungadmin?ID=101".

我在项目的其他网站上有很多相同的表格,但都在工作,只是这个没有。

我的控制器:

public function actionBestellungadmin(){
        if(Yii::$app->user->isGuest){
            return $this->goHome();
        }else if(Yii::$app->user->identity->getAdmin()){
            $model= new Bestellung();
            $model->load(Yii::$app->request->get());
            if($model->load(Yii::$app->request->post())){
                $model->updateBestellung();
                Yii::$app->session->setFlash('success', 'Bestellung erfolgreich aktuallisiert.');
                return $this->redirect('bestellungenoffen');
            }else if(Yii::$app->request->get('ID')){

                        $ID = Yii::$app->request->get('ID');
                        $query = new Query;
                        // compose the query
                        $query->select('*')
                                ->from('bestellung')
                                ->where("ID = '$ID'");
                        // build and execute the query
                        $row = $query->all();                       
                        return $this->render('bestellungadmin', ['model'=>$model, 'output'=>$row]);
            }else{
                return $this->render('bestellungenoffen');
            }
        }else{
            return $this->goHome();
        }
    }

我的表格:

<?php 
                foreach($output as $row){
                    echo "<div class='row artikelAdmin'>";
                $form = ActiveForm::begin(['options'=>['enctype'=>'multipart/form-data']]); 

                echo $form->field($model, 'Nummer')->textInput(['readonly' => true, 'value' => $row['Nummer']]);
                echo $form->field($model, 'Datum')->textInput(['readonly' => true, 'value' => $row['Datum']]);
                echo $form->field($model, 'Artikel')->textInput(['readonly' => false, 'value' => $row['Artikel']]);
                echo "<a href='".$row['Rechnung']."'>Rechnung</a>";
                echo $form->field($model, 'Anmerkung')->textArea(['readonly' => true, 'value' => $row['Anmerkung']]) ;
                echo $form->field($model, 'Email')->textInput(['readonly' => false, 'value' => $row['Email']]) ;
                echo $form->field($model, 'Adresse')->textInput(['readonly' => false, 'value' => $row['Adresse']]) ;
                echo $form->field($model, 'PreisArtikel')->textInput(['readonly' => false, 'value' => $row['PreisArtikel']]) ;
                echo $form->field($model, 'PreisGesamt')->textInput(['readonly' => false, 'value' => $row['PreisGesamt']]) ;
                echo $form->field($model, 'Status')->dropDownList(['Storniert' => 'Storniert','Warten auf Zahlungseingang'=> 'Warten auf Zahlungseingang', 'Zahlung erhalten, Versandvorbereitung' => 'Zahlung erhalten, Versandvorbereitung', 'Bestellung verschickt' => 'Bestellung verschickt'],['options'=>[$row['Status']=>['Selected'=>true]]])->label('Status Ändern');
                echo $form->field($model, 'Nachricht')->textArea(['readonly' => false]) ;
                echo "<div class='form-group'>";
                echo Html::submitButton("speichern", ['class' => 'btn btn-primary ', 'name' => 'speichern']) ;
                echo "</div>";
                ActiveForm::end(); 
                echo "</div>";
                }
                ?>

【问题讨论】:

  • 您使用的是默认表单提交还是 ajax ?还有你使用的是什么版本的jquery

标签: php forms yii2 submit


【解决方案1】:

从表单选项中删除 enctype。 (您不需要指定任何 enctype 选项 - Yii 将使用默认值)。

Multiform 用于上传,因此是您的问题。

其他cmets: 您的操作显然使用了变量 $ID,因此应在函数中指定如下:

public function actionBestelling($id = null){

这样做会自动为您填充 $id ,从而使编码更容易。

$row 可以改写为:

$row = Bestellung::findAll([‘id’=>$id]); 

这假设有多个具有相同 ID 的行......这很奇怪。如果不是这种情况,请使用:

$row = Bestellung::findOne($id);

如果确实只有一个ID,那么也可以去掉视图文件中的foreach语句。

【讨论】:

  • 非常感谢。我改变了这个,现在它正在工作!我只是无法重写 $row = Bestellung::findOne($id);因为 bei Model Bestellung 仍然扩展了模型 Model。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多