Magento – Form per Ajax Request absenden
Hier ein Beispiel (Produkt zum Warenkorb hinzufügen) zum absenden eines Form per Ajax Request in Magento:
<script type="text/javascript"> //<![CDATA[ var productAddToCartForm = new VarienForm('product_addtocart_form'); productAddToCartForm.submit = function(product_name) { if (this.validator.validate()) { new Ajax.Request($('product_addtocart_form').action, { parameters: Form.serialize($('product_addtocart_form'), true), area: $('product_addtocart_form'), onComplete: function (transport) { } }); } }.bind(productAddToCartForm); //]]> </script>
Alternative Code aus dem Newsletter
<script type="text/javascript"> //<![CDATA[ function submitSubscribe() { new Ajax.Request('<?php echo Mage::getBaseUrl() ?>newsletter/subscriber/newajax/', { method:'post', parameters: $('newsletter-subscribe').serialize(true), onLoading: function(){ $('subscribe_update_div').show(); $('subscribe_update_div').innerHTML = "<?php echo $this->__('Sending subcribe request ...'); ?>"; }, onSuccess: function(transport){ var response = transport.responseText.evalJSON(); $('subscribe_update_div').innerHTML = response.message; }, onFailure: function(){ $('subscribe_update_div').innerHTML = "<?php echo $this->__('Something went wrong ...'); ?>"; } }); } //]]> </script>