duan1982453 2013-09-29 05:35
浏览 30
已采纳

在按钮上调用控制器动作的问题单击

I'm having a problem calling an action in a controller upon button click. So the controller is generated by Gii. All of its actions are the default ones generated by Gii, except for the actionCreate().

Here is the relevant code ::

class ProductsController extends Controller {
 public function actionCreate() {
        $model = new Products;



      if (isset($_POST['params'])) {
        //  $model->attributes = $_POST['Products'];
        //if ($model->save())
         //   $this->redirect(array('view', 'id' => $model->id));
         echo 'Yes Working';
    }

    $this->render('create', array(
        'model' => $model,
    ));
}

As its clear from the above code snippet this action is calling the view named create.php. Here is create.php::

<div class="page">
<div class="container">
    <div class="row">
    <h2>Create Products</h2>

    <?php echo $this->renderPartial('_form', array('model' => $model)); ?>
    </div>
</div>

And here is the partially rendered form.

<?php
$form = $this->beginWidget('bootstrap.widgets.TbActiveForm', array(
    'id' => 'products-form',
    'action' => Yii::app()->createUrl('products/create'),
    'enableAjaxValidation' => false,
        ));
?>

<div class="form-actions">
    <?php
    echo CHtml::submitButton('Create', array(
        'submit' => 'EasyAesthetics/index.php/products/create',
        'params' => '1'
    ));
    ?>
</div>

<?php $this->endWidget(); ?>

Now what I want is that upon clicking the button 'Create', it would call the actionCreate() method in the ProductsController. Right now the button is working and I'm being redirected to /demoProject/index.php/products/create, but the echo 'Yes Working' is not displaying.

Can anyone please show me how to achieve this. How can i invoke the create action again with just a button and just a 1 in the $_POST array.

I need to do this so that on clicking create the actionCreate() method will call the relevant components to create the necessary products.

  • 写回答

2条回答 默认 最新

  • douguaidian8021 2013-09-29 06:12
    关注

    Take a look at the code generated by your form. When you have model called "Hello" with a field called "world", your form field will be

    <input type="text" name="Hello[world]">
    

    Try to change your action in this way:

    class ProductsController extends Controller {
        public function actionCreate() {
            $model = new Products;
            if (isset($_POST['Products'])) {
                echo 'Yes Working';
            }
            $this->render('create', array(
                'model' => $model,
            ));
        }
    }
    

    Pay particular attention to these two lines:

            $model = new Products;
            if (isset($_POST['Products'])) {
    

    Fields will takes the same name of model. In case of more models:

    <input type="text" name="Model1[field1]">
    <input type="text" name="Model1[field2]">
    <input type="text" name="Model21[field2]">
    <input type="text" name="Model2[field2]">
    

    and so on ...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab