0

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.

Renaissance
  • 798
  • 5
  • 15
Maxx
  • 592
  • 18
  • 42

2 Answers2

1

if your "var_dump()"ed your "$_POST" , you would see sensorario answer.

and also you can set your froms send method to post if still not sending post.

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

?>

or get your parameter like this(this sets by $_REQUEST):

$param = Yii::app()->request->getParam('Products' , null);
Developerium
  • 7,155
  • 5
  • 36
  • 56
  • Thanks for you answer. I did this previously but the data is not submitting. The check for if the POST array is set is not becoming true. – Maxx Sep 29 '13 at 15:43
  • your submit button might be the problem, change its type or something. – Developerium Sep 30 '13 at 04:54
  • Actually it works now. echo CHtml::submitButton('Create', array( 'submit' => 'EasyAesthetics/index.php/products/GetData', 'params' => '1' )); – Maxx Sep 30 '13 at 05:21
  • the GetData() action is now being called flawlesslly. – Maxx Sep 30 '13 at 05:22
0

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 ...

sensorario
  • 20,262
  • 30
  • 97
  • 159
  • Thanks a lot for you reply. Its working now. Somehow i thought that the POST array would be indexed using 'params' as the key, like in an associative array. Anyways, i was wrong. Thanks, again for this. I really appreciate it. – Maxx Sep 29 '13 at 15:46