Magento 1.9 – How to Reload Page on Button Click

addtocartajaxjquerymagento-1.9zipcode

I am building an ecommerce magento site named www.bookslab.in. I had one issue in this website. I want to make compulsory for every user to check their zipcode before they buy anything from the website, because i am delivering the products in the limited area. Now i had added the exception for that. The exception is when user's zipcode is invalid then ADD_TO_CART button is disabled, this is the exception. So, the problem comes here when user checks that the zipcode he enters is wrong, so after that he enters the correct zipcode, but still the ADD_TO_CART button is disabled. To solved this issue, i want to refresh(reload) the page everytime when user clicks on CHECK button of zipcode field. Plz suggest me in the code i provided & go to my website to see the problem.

cod.phtml

<?php
/***************************************
 *** Cash On Delivery ***
 **************************************
 */

$msgData = Mage::helper('netgo_cod')->getConfigData(); 
if($msgData['zip_status'] == 1){
?>
<div class="input-box">
    <div class="z-btn">
        <label>Zip Code : </label><input type="text" placeholder="Enter your pincode" value="" name="cod" class="product-custom-option required-entry" id="cod" size="29" style='color:black; font-size:12px'>
        <button type="button" onclick="checkCOD();" name="zip-check" title="Check" class="button" id="zip-check"><span><span>Check</span></span></button>
    </div>
    <div id="cod_msg"></div>
</div>

<script>
    function checkCOD(){
             var zipcode;
        var cod = $('cod').value;
        if(cod == ''){
            $('cod_msg').update('<span class="cod-error"><?php echo $msgData['emp_msg']; ?></span>');
            return
        }else{
            $('cod_msg').update('<img src="<?php echo Mage::getBaseUrl( Mage_Core_Model_Store::URL_TYPE_WEB, true ).'media/cod/img/ajax-loader-2.gif';?>">'); 
            new Ajax.Request('<?php echo Mage::getBaseUrl().'netgo/cod/check';?>', {
                method:'post',
                parameters: {zipcode: cod}, 
                onSuccess: function(transport) {
                    var response = transport.responseText || "no response text";
                    $('cod_msg').update(response);

                    if(response.search('Delivery of the book is not available on the above pincode')!=-1)
                                        {
                                          zipcode='checked';
                                          jQuery('.btn-cart').attr('disabled', 'disabled'); 
                                        }
                                      else
                                      {
                                      zipcode='';

                                      }

                },
                onFailure: function() { alert('Something went wrong...'); }
            });
        }
    }
</script>
<?php } ?>

Screenshot:

enter image description here

Best Answer

Please try after replacing your script code with the following code. You don't need to refresh the page to enable the button.

<script>
    function checkCOD(){
             var zipcode;
        var cod = $('cod').value;
        if(cod == ''){
            $('cod_msg').update('<span class="cod-error"><?php echo $msgData['emp_msg']; ?></span>');
            return
        }else{
            $('cod_msg').update('<img src="<?php echo Mage::getBaseUrl( Mage_Core_Model_Store::URL_TYPE_WEB, true ).'media/cod/img/ajax-loader-2.gif';?>">'); 
            new Ajax.Request('<?php echo Mage::getBaseUrl().'netgo/cod/check';?>', {
                method:'post',
                parameters: {zipcode: cod}, 
                onSuccess: function(transport) {
                    var response = transport.responseText || "no response text";
                    $('cod_msg').update(response);

                    if(response.search('Delivery of the book is not available on the above pincode')!=-1)
                                        {
                                          zipcode='checked';
                                          jQuery('.btn-cart').attr('disabled', 'disabled'); 
                                        }
                                      else
                                      {
                                       jQuery('.btn-cart').removeAttr('disabled');

                                      }

                },
                onFailure: function() { alert('Something went wrong...'); }
            });
        }
    }
</script>

Now when the user will enter the correct code, the add to cart button will automatically get enabled without refreshing the page.

Related Topic