mosonyi
(Mosonyi)
February 16, 2010, 11:56pm
1
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!
jmc
(Buyakashal)
February 17, 2010, 11:58am
2
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
mosonyi
(Mosonyi)
February 17, 2010, 12:39pm
3
JMC:
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!
bettor
(Live Webscore)
February 17, 2010, 1:07pm
4
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!
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
mosonyi
(Mosonyi)
February 17, 2010, 1:23pm
5
bettor:
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
jmc
(Buyakashal)
February 17, 2010, 1:28pm
6
And if you see the source of your form, the piece1 is there as an "<input type="text" name="piece1" />" or not?
mosonyi
(Mosonyi)
February 17, 2010, 1:31pm
7
<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.
bettor
(Live Webscore)
February 17, 2010, 1:41pm
8
can you give us print_r($_POST); result after you initiate ajax call?
jmc
(Buyakashal)
February 17, 2010, 1:42pm
9
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)
mosonyi
(Mosonyi)
February 17, 2010, 1:50pm
11
JMC:
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)
It’s in an other Controller. The actionAdd is called I could track this.
mosonyi
(Mosonyi)
February 18, 2010, 3:26pm
12
Has anyone a working example? Thanks.
bettor
(Live Webscore)
February 18, 2010, 3:50pm
13
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:
$product->product_id is unique for each product and is rendered only once in a page
put your id of the element that you want to update against the ‘update’ field (below the POST)
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!
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
mosonyi
(Mosonyi)
February 22, 2010, 10:57pm
14
bettor:
Hi! another attempt:
I suspect there might be something wrong with your other code. Please make sure the following:
$product->product_id is unique for each product and is rendered only once in a page
put your id of the element that you want to update against the ‘update’ field (below the POST)
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!
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.
bettor
(Live Webscore)
February 22, 2010, 11:26pm
15
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
bettor
(Live Webscore)
February 22, 2010, 11:28pm
16
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
mosonyi
(Mosonyi)
February 22, 2010, 11:42pm
18
Please see the attachement. Thanks
nadeems
(Nadyshaikh)
May 18, 2011, 12:53pm
20
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