dimis283
(Dimis283)
1
I use this form (and below the action).
My problem is that the ajax request seems not to working what is wrong?
echo "<form name='".$item['id']."' id='".$item['id']."' action='".CHtml::normalizeUrl(array('card/RemoveElement'))."'
method='post'><input type='hidden' id='idd'
name='idd' value='".$item['id']."'/>";
echo CHtml::ajaxSubmitButton('remove', CHtml::normalizeUrl(array('card/RemoveElement')), array('replace'=>'doing'));
echo "</form>";
public function actionRemoveElement()
{ if (!empty($_POST['idd']))
{
$this->actionRemove(intval($_POST['idd']));
}
print_r($this->cart);
}
pestaa
(Pestaa)
2
Seems quite strange, since action methods normally don’t have arguments, yet you want to pass an int to actionRemove.
Refactor your code so that actionRemoveElement calls a model to delete itself.
dimis283
(Dimis283)
3
I make a model cart for the cart.
But at my page I see that the request is not doing.
The code that yii writes is
jQuery('#yt2').click(function(){jQuery.ajax({'type':'POST','url':'/book_store/index.php/Card/Add',
'cache':false,'data':jQuery(this).parents("form").serialize(),
'success':function(html){
jQuery("doing").replaceWith(html)}});return false;});
As I see parents find all elements of dom and at this occassion the elements those are ‘form’ (as I understand).
At this page there are many forms, so I can not use this function of Yii?
I change the javascript code as
jQuery('#yt1').click(function(){jQuery.ajax({'type':'POST','url':'/book_store/index.php/Card/Add',
'cache':false,'data':jQuery("#form").serialize(),'success':function(html){alert(html);}});return false;});
And it worked for me.
pestaa
(Pestaa)
4
Is there any chance you accidentally implemented nested form tags?
As of jQuery documentation, parents() should not conflict with separate forms.
Furthermore, I see you changed #yt2 to #yt1, which also doesn’t make sense if you used forms correctly.
dimis283
(Dimis283)
5
I manage to run the script but now the update is here for just the first time.What is wrong?
Edit:Also the remove function run only if the page is not been "updated",how can I solve this?
subcard
<?php
echo "<h2>Items in cart</h2>";
echo "<div id='card'>";
if($cart->get_itemcount() > 0) {
foreach($cart->get_contents() as $item) {
echo "<br />Item:<br/>";
echo "Code/ID :".$item['id']."<br/>";
echo "Quantity:".$item['qty']."<br/>";
echo "Price :$".number_format($item['price'],2)."<br/>";
echo "Info :".$item['info']."<br />";
echo "Subtotal :$".number_format($item['subtotal'],2)."<br />";
echo CHtml::beginForm(CHtml::normalizeUrl('Card/RemoveElement'),'post');
// echo "<form name='".$item['id']."' id='".$item['id']."' action='".CHtml::normalizeUrl(array('card/RemoveElement'))."' method='post'>
echo "<input type='hidden' id='id' name='id' value='".$item['id']."'/>";
echo CHtml::ajaxSubmitButton('remove', CHtml::normalizeUrl(array('card/RemoveElement')), array('update'=>'#doing'));
echo "</form>";
}
echo "---------------------<br>";
echo "total: $".number_format($cart->get_total(),2);
} else {
echo "No items in cart";
}
echo "</div >";?>