【问题标题】:Having Issues Invoking Actions Of Controller On Button Click在按钮单击时调用控制器的操作时出现问题
【发布时间】:2013-09-29 05:35:30
【问题描述】:

我在单击按钮时调用控制器中的操作时遇到问题。所以控制器是由Gii生成的。它的所有动作都是 Gii 生成的默认动作,除了actionCreate().

这里是相关代码::

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,
    ));
}

从上面的代码 sn-p 中可以清楚地看出,这个动作调用了名为 create.php 的视图。 这里是 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>

这里是部分渲染的表格。

<?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(); ?>

现在我想要的是,在单击“创建”按钮后,它将调用 ProductsController 中的 actionCreate() 方法。现在按钮正在工作,我被重定向到/demoProject/index.php/products/create,但没有显示回显“Yes Working”。

谁能告诉我如何做到这一点。如何仅使用一个按钮和 $_POST 数组中的 1 再次调用创建操作。

我需要这样做,以便在单击创建时actionCreate() 方法将调用相关组件来创建必要的产品。

【问题讨论】:

    标签: php forms yii gii


    【解决方案1】:

    如果你的 "var_dump()" 编辑了你的 "$_POST" ,你会看到 sensorario 的答案。

    如果您仍然不发送帖子,您也可以将您的 froms 发送方法设置为发布。

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

    ?>

    或像这样获取您的参数(由 $_REQUEST 设置):

    $param = Yii::app()->request->getParam('Products' , null);
    

    【讨论】:

    • 感谢您的回答。我以前这样做过,但数据没有提交。检查是否设置了 POST 数组未变为真。
    • 你的提交按钮可能有问题,改变它的类型什么的。
    • 实际上它现在可以工作了。 echo CHtml::submitButton('Create', array('submit' => 'EasyAesthetics/index.php/products/GetData', 'params' => '1' ));
    • GetData() 操作现在被完美调用。
    【解决方案2】:

    查看表单生成的代码。当你有一个名为“Hello”的模型和一个名为“world”的字段时,你的表单字段将是

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

    尝试以这种方式改变你的行为:

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

    特别注意这两行:

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

    字段将采用与模型相同的名称。如果有更多型号:

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

    等等……

    【讨论】:

    • 非常感谢您的回复。它现在工作。不知何故,我认为 POST 数组将使用“参数”作为键进行索引,就像在关联数组中一样。反正我错了。谢谢,再次为此。我真的很感激。
    猜你喜欢
    • 2018-11-26
    • 2018-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-09
    • 2011-02-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多