简体   繁体   中英

how to display success or error alert in same page without redirecting

I am trying to do pincode validation in product page. Its working but after submiting pincode its redirecting to new page and alert also coming in new page.

I need to display alert in same product page without redirect

Help me..

Code:

app/code/local/purna/Zipcode/controllers/indexcontroller.php

class Purna_Zipcode_IndexController extends Mage_Core_Controller_Front_Action
{
    public function zipcodeAction() {
        $data = $this->getRequest()->getParams();
        $session = Mage::getSingleton('core/session');
        $zipcode = $data['zipcode'];
        $collection = Mage::getModel('zipcode/zipcode')
                         ->getCollection()
                         ->addFilter('zipcode', $zipcode, '=')
                         ->getData();

        if(count($collection) > 0){
            echo "<script>alert(not available);</script>";
        } else {
            echo "<script>alert(available);</script>"; 
        }
    }
}

app/design/frontend/base/default/template/zipcode/index/zipcode.phtml

<form action="<?php echo Mage::getUrl('zipcode/index/zipcode') ?>" method="post" id="zipcode-submit">
    <input type="text" name="zipcode" class="input-text" id="zipcode">
    <button type="submit" class="button">Submit</button>
</form>

app/design/frontend/base/default/layout/zipcode.xml

<?xml version="1.0"?>   
   <layout version="0.1.0">   
      <zipcode_index_index>   
          <reference name="root">   
             <action method="setTemplate">
             <template>page/1column.phtml</template></action>   
          </reference>   
          <reference name="content">   
              <block type="zipcode/index" name="zipcode_index" template="zipcode/zipcode.phtml"/>   
          </reference>   
      </zipcode_index_index>   
   </layout>

app/design/frontend/default/theme/template/catalog/product/view.phtml

<?php 
    echo $this->getLayout()
              -> CreateBlock('core/template')
              ->setTemplate('zipcode/index/zipcode.phtml')
              ->toHtml()
?>

from your code your xml will load form on the zipcode_index_index action. can you tell mw how you are using the form on product page.

but to achieve your solution you must use a magento error messages

$data = $this->getRequest()->getParams();
        $session = Mage::getSingleton('core/session');
        $zipcode = $data['zipcode'];
        $collection = Mage::getModel('zipcode/zipcode')
                         ->getCollection()
                         ->addFilter('zipcode', $zipcode, '=')
                         ->getData();

        if(count($collection) > 0){
            Mage::getSingleton('core/session')->addError($this->__('not available'));
        } else {
    Mage::getSingleton('core/session')->addSuccess($this->__('available'));
        }

    $this->_redirectReferer(); 

please check the code it will also solve your redirection problem. $this->_redirectReferer(); // this wiil do a redirection to a referrer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM