1

I want to submit my form on the same page :

<form action="<?php echo Mage::helper('checkout/cart')->getAddUrl(Mage::registry('current_product')); ?>" method="post" id="seller_addtocart_form1" class="ma">
    <button id="<?php echo $seller->getMpassignproductId() ?>" class="button btn-cart customaddtocart" title="<?php echo $this->__('Add to Cart')?>" onclick="<?php echo $currentUrl = Mage::helper('core/url')->getCurrentUrl();?>">
    <?php echo $this->__('Add to Bag')?></span></span></button>
</form>

Form is submitted in cart page so I use onclick() function in button but it won't work.

Can anyone please guide me, what's wrong in my code ?

3
  • What is your objectif to submit the form in the same page, why you dont use $this->_redirectReferer() in your controller ?
    – PЯINCƎ
    Commented Aug 15, 2017 at 21:19
  • You shouldn't be using inline js. Also you should be using Magento js methods to submit the form. I'll see if I can whip up an answer for you. Commented May 1, 2018 at 3:55
  • You say 'Form is submitted in cart page'. If you are on the cart page, then Mage::registry('current_product') should return null. Can you please clarify what you mean? Commented May 1, 2018 at 4:02

1 Answer 1

0

You need follow below steps

  • add return_url as a new hidden input field to from

<input type="hidden" name="return_url" value="<?php echo $currentUrl = Mage::helper('core/url')->getCurrentUrl();?>" />

  • and remove onclick="<?php echo $currentUrl = Mage::helper('core/url')->getCurrentUrl();?>" for form submit

<button id="<?php echo $seller->getMpassignproductId() ?>" class="button btn-cart customaddtocart" title="<?php echo $this->__('Add to Cart')?>" type="submit">

from

<button id="<?php echo $seller->getMpassignproductId() ?>" class="button btn-cart customaddtocart" title="<?php echo $this->__('Add to Cart')?>" onclick="<?php echo $currentUrl = Mage::helper('core/url')->getCurrentUrl();?>">

Update:

Use this it should works:

 <form action="<?php echo Mage::helper('checkout/cart')->getAddUrl(Mage::registry('current_product')); ?>" method="post" id="seller_addtocart_form1" class="ma">

<input type="hidden" name="return_url" value="<?php echo $currentUrl
  =  Mage::helper('core/url')->getCurrentUrl();?>" />

    <button id="<?php echo $seller->getMpassignproductId() ?>" class="button btn-cart customaddtocart" title="<?php echo $this->__('Add to Cart')?>" type="submit"><span><span><?php echo $this->__('Add to Bag')?></span></span></button></form>
3
  • Where i have to add form or button. i tried both separately but it redirect to shopping cart only
    – Magento 2
    Commented Nov 4, 2015 at 5:08
  • add to cart Working but i used Buy now it should redirect to shopping cart but now it also submit same page it self
    – Magento 2
    Commented Nov 4, 2015 at 5:27
  • <button class="button buy-now" onClick="location.href ='{{config path="web/secure/base_url"}}/checkout/cart/add?product=1&qty=1'"> <span><span>Buy Now</span></span></button>
    – Magento 2
    Commented Nov 4, 2015 at 5:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.