0

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 ?

  • What is your objectif to submit the form in the same page, why you dont use $this->_redirectReferer() in your controller ? – PЯINCƏ Aug 15 '17 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. – Shawn Abramson May 1 '18 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? – Shawn Abramson May 1 '18 at 4:02
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>
  • Where i have to add form or button. i tried both separately but it redirect to shopping cart only – Magento 2 Nov 4 '15 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 Nov 4 '15 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 Nov 4 '15 at 5:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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