Pages

Wednesday 12 November 2014

Magento Pincode varification to add to cart.

1- Take the html layout where you want to the html layout.
<div class="pincode">
<input type="text" name="pincode" id='pincode1'  placeholder="Enter pincode to check dilivery option">
<div id='pincode'></div>
<input class="verify-button" type='button' onclick="checkForm()" value='Verify'>
</div>

2- The jquery for validation.
<script type="text/javascript">
function checkForm()
{
    //fetching value from input field pincode  and storing it in a variable
    var pincode = document.getElementById("pincode1").value;
    //alert(pincode);
    //Check input Fields Should not be blanks.
    if (pincode == '' )  
    {
        jQuery('#pincode').html('<div style="color:red;">Enter The Pincode</div>');
    }
    else
    {
        jQuery.ajax({
            url: "<?php echo $this->getBaseUrl();?>ajax/pincode_verify.php",
            type: "POST",
            data:'pincode='+pincode,
            success: function(abc){
            //alert(abc);
                if(abc==1){
                    jQuery(".add-to-box").show();
                    jQuery('#pincode').html('<div style="color:green;">This pincod is available</div>');
                }
                else{
                    jQuery(".add-to-box").hide();
                    jQuery('#pincode').html('<div style="color:red;">This pincod is not available</div>');
    
                }  
            }
        });
    }
}
</script>

3- create a folder name ajax in magento root directory and the inside it the pincode_verify.php for validation.
<?php
require_once '../app/Mage.php';
Mage::app('default');

extract($_REQUEST);
$read = Mage::getSingleton( 'core/resource' )->getConnection( 'core_read' );
$available_pincodes = Mage::getSingleton( 'core/resource' )->getTableName( 'available_pincodes' );
$query = "SELECT * FROM " . $available_pincodes." WHERE pincode_value = '".$pincode."'" ;
$result = $read->fetchAll( $query );

if(count($result) > 0)
echo 1;
else
echo 0;
?>

No comments:

Post a Comment