ajaxsubmitbutton and $_POST is empty?

This was the first time I tried to use ajaxsubmitbutton. No success yet:(

My view:




<?php CHtml::beginForm(); ?>

            <?php foreach($products as $product): ?>

                <?php echo CHtml::textField('piece'.$product->product_id)?>

                  <?php echo CHtml::ajaxSubmitButton('DoAjaxRequest', Yii::app()->controller->createUrl("basket/add"), array('type'=>'POST') ); ?>

            <?php endforeach; ?>

<?php CHtml::endForm(); ?>



And the controller




    public function actionAdd() {

        if(Yii::app()->request->isAjaxRequest) {

            Yii::log("Add product to basket:".$_POST['piece1'], CLogger::LEVEL_INFO );

            // -> [error] [php] Undefined index:  piece1

        } else {

            echo $_POST['piece1']; exit; //-> it works if I use submitbutton

        }

    }



I use the latest yii. Please help me!;)

you might want to add the update part to your ajaxSubmitButton




<?php CHtml::beginForm(); ?>

            <?php foreach($products as $product): ?>

                <?php echo CHtml::textField('piece'.$product->product_id)?>

                  <?php echo CHtml::ajaxSubmitButton('DoAjaxRequest',      

                      Yii::app()->controller->createUrl("basket/add"), 

                      array('type'=>'POST',

                            'update'=>'#basketdiv',

                      ) ); ?>

            <?php endforeach; ?>

<?php CHtml::endForm(); ?>



and if you use firebug for firefox, you can control your ajax actions EZ ;)

Maybe you misunderstood me. the $_POST[‘piece1’] is undefined index.

I checked with many FF plugins. The ajaxsubmitbutton does NOT send the POST datas.

If you have a working example please attach it. Thanks!

Hi,

I suspect that your form displays more than one product and the way you have defined your ajax button leads to multiple ajax buttons with the same name in your form causing the buttons to not work. try setting up each button with different names such as:


<?php CHtml::beginForm(); ?>

            <?php foreach($products as $product): ?>

                <?php echo CHtml::textField('piece'.$product->product_id)?>

                  <?php echo CHtml::ajaxSubmitButton('DoAjaxRequest', Yii::app()->controller->createUrl("basket/add"), array('type'=>'POST'),array('name'=>CHtml::encode('button'.$product->product_id)) ); ?>

            <?php endforeach; ?>

<?php CHtml::endForm(); ?>

[not tested just a stab in the dark]

also can you paste the params as they appear in your FF/Net…you can also try to accept the post in your controller with

if(Yii::app()->request->isPostRequest) for more debugging on the post type

Thanks for the tips, I tried it but this was not the solution:(

…Undefined index: piece1

And if you see the source of your form, the piece1 is there as an "<input type="text" name="piece1" />" or not?

<input id="piece1" type="text" name="piece1" value=""/>

I can see it. The strange thing: If I use normal submit button I get the POST data. via ajaxsubmitbutton I don’t.

can you give us print_r($_POST); result after you initiate ajax call?

may in your view, where you define your ajax options

instead of this:

Yii::app()->controller->createUrl("basket/add"),

use this:

CController::createUrl(‘add’),

and be careful, because the "add" action is casesensitive in your controller (public funcion actionAdd(){…)

and im not sure, how your layout looks like, but i think, the add action should be in the same controller, which renders the main layout. (thats just a guess)

print_r($_POST);

Array

(

)

print_r($_GET);

Array

(

[r] =&gt; basket/add

)

It’s in an other Controller. The actionAdd is called I could track this.

Has anyone a working example? Thanks.

Hi! another attempt:


<?php echo CHtml::beginForm(); ?>

            <?php foreach($products as $product): ?>

                <?php echo CHtml::textField('piece'.$product->product_id)?>

                  <?php echo CHtml::ajaxSubmitButton('DoAjaxRequest', CController::createUrl("basket/Add"), 

				  array(

					'type'=>'POST',

					'update'=>'put the id of the element to be updated',

				  ), 

				  array(

                    'name' => CHtml::encode('button1'.$product->product_id),

                         )

				  ); ?>

            <?php endforeach; ?>

<?php echo CHtml::endForm(); ?>

I suspect there might be something wrong with your other code. Please make sure the following:

  1. $product->product_id is unique for each product and is rendered only once in a page

  2. put your id of the element that you want to update against the ‘update’ field (below the POST)

  3. before you click on the button if there are more than one button use firebug to make sure that every button has a different name!

  4. make sure that the name of the controller [basket] and the action name [Add] are correct.

If all above is fulfilled and this still does not work than you will have to attach some of your files to have a look at.

Good luck and let me know the result.

Best,

bettor

Thanks bettor, I am sure I did everything as you wrote.

I tried this and it is a bit strange:




...

                  <?php echo CHtml::ajaxSubmitButton('DoAjaxRequest', 

                          Yii::app()->controller->createUrl("basket/add"),

                        array (

                            'beforeSend' => 'function(){

                              alert(jQuery(this).parents("form").serialize());}',

                            

                            )); ?>

...



The alert window is empty.

very weird. If you want send me your model,controller and view file to have a look. I cannot think of anything else…the way I have laid it out works for me :( … quite weird situation

I can also see that in your initial post you have made a mistake [I think]


<?php CHtml::beginForm(); ?>

…you should echo your form…did you amend that as well?

cheers,

bettor

sure I found it also.

Please see the attachement. Thanks

548

shop.zip

hello

i am also having the same issue if you got any solution can you post that here that means if anyone will face this same problem they will have solution from here

thanks in advance