Having Issues Invoking Actions Of Controller On Button Click

Hi, 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>

</div>

And here is the partially rendered form.


<?php $form=$this->beginWidget('bootstrap.widgets.TbActiveForm',array(

	'id'=>'products-form',

        


	'enableAjaxValidation'=>false,

)); ?>








<div class="form-actions">

	<?php

         echo CHtml::submitButton('Create', array(

                                                'submit'=>'demoProject/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.

Thanks, in advance. I would really appreciate if someone can atleast show me the correct way to do this.

Thanks,

Maxx

Please change this url

‘submit’=>‘demoProject/index.php/products/create’,

to

‘submit’=>‘demoProject/index.php?r=products/create’

try like this

hi Maxx,

try this in view:




echo CHtml::submitButton('Create', array(

  'submit'=>$this->createUrl('products/create'),

  'params'=>array('id1'=>'value1','id2'=>'value2',...)

));



and in the Controller:




 if (isset($_POST['params'])) {

  ...

 }



change it for :




 if (isset($_POST['Products'])) {// if you want by $_POST['id1']

  ...

 }



Thanks for your reply. Actually i’ve the friendly URL setting on in main.php, so my URL is automatically rendered in that manner. i.e. hostname/index.php/controller/actionName.

Yes it works now. I thought that the post array would be indexed using params as the key, but i was wrong. Anyways, thanks a lot for your answer, i really appreciate it.