Magento – How to we compare email address from database and form

magento-1.7

i make one form in which i have first name, last name and email address. now, when i press

submit button on form it would be check with database if it is exist in database? then

it will show error message like "Email address already exist".

=> app\code\local\Xyz\Example\controllers\IndexController.php

<?php
class Xyz_Example_IndexController extends Mage_Core_Controller_Front_Action
{
    public function indexAction()
    {
      $this->loadLayout();
      $this->renderLayout();
    }

    public function postAction()
    {
                /* $post = $this->getRequest()->getParams();
                $model = Mage::getModel('example/example');
                $model->setData($post);
                $model->save();
                Mage::getSingleton('core/session')->addSuccess('Successfully save data...');

      $this->_redirectReferer();
    }
}
?>

=> app\design\frontend\default\example_theme\template\example\email2.phtml

this is my form file…

<div id="messages_product_view"><?php echo $this->getMessagesBlock()->getGroupedHtml() ?></div>
<div class="page-title">
    <h1><?php echo Mage::helper('contacts')->__('E-Mail') ?></h1>
</div>
<form action="<?php echo $this->getBaseUrl().'example/index/post'; ?>" id="contactForm" method="post">
    <div class="fieldset">
        <h2 class="legend"><?php echo Mage::helper('contacts')->__('Contact Information') ?></h2>
        <ul class="form-list">
            <li class="fields">
                <div class="field">
                    <label for="firstname" class="required"><em>*</em><?php echo Mage::helper('contacts')->__('First Name') ?></label>
                    <div class="input-box">
                        <input name="fname" id="firstname" title="<?php echo Mage::helper('contacts')->__('Name') ?>" value="<?php echo $this->htmlEscape($this->helper('contacts')->getUserName()) ?>" class="input-text required-entry" type="text" />
                    </div>
                </div>
                <div class="field">
                    <label for="lastname" class="required"><em>*</em><?php echo Mage::helper('contacts')->__('LastName') ?></label>
                    <div class="input-box">
                        <input name="lname" id="lastname" title="<?php echo Mage::helper('contacts')->__('Name') ?>" value="<?php echo $this->htmlEscape($this->helper('contacts')->getUserName()) ?>" class="input-text required-entry" type="text" />
                    </div>
                </div>
                <div class="field">
                    <label for="email" class="required"><em>*</em><?php echo Mage::helper('contacts')->__('Email') ?></label>
                    <div class="input-box">
                        <input name="email" id="email" title="<?php echo Mage::helper('contacts')->__('Email') ?>" value="<?php echo $this->htmlEscape($this->helper('contacts')->getUserEmail()) ?>" class="input-text required-entry validate-email" type="text" />
                    </div>
                </div>


                <div class="field">
                    <label for="Date" class="required"><em>*</em><?php echo Mage::helper('contacts')->__('Date') ?></label>
                    <div class="input-box">
                        <input name="date" id="date" title="<?php echo Mage::helper('contacts')->__('Date') ?>" value="<?php $date = new Zend_Date(); $createdDate= $date->get('dd-MM-YYYY'); print($createdDate); ?>" type="text"  />
                    </div>
                </div>
            </li>
        </ul>
    </div>
    <div class="buttons-set">
        <p class="required"><?php echo Mage::helper('contacts')->__('* Required Fields') ?></p>
        <input type="text" name="hideit" id="hideit" value="" style="display:none !important;" />
        <button type="submit" title="<?php echo Mage::helper('contacts')->__('Submit') ?>" class="button"><span><span><?php echo Mage::helper('contacts')->__('Submit') ?></span></span></button>
    </div>
</form>
<script type="text/javascript">
//<![CDATA[
    var contactForm = new VarienForm('contactForm', true);
//]]>
</script>

=> app\code\local\Xyz\Example\Model\Example.php

<?php
class Xyz_Example_Model_Example extends Mage_Core_Model_Abstract
{
    public function _construct()
    {
        parent::_construct();
        $this->_init('example/example'); // this is location of the resource file.
} 
}
?>

=> app\code\local\Xyz\Example\sql\example_setup\mysql4-install-0.1.0.php

<?php
$installer = $this;  //Getting Installer Class Object In A Variable
$installer->startSetup();
$installer->run("
-- DROP TABLE IF EXISTS {$this->getTable('example')};
CREATE TABLE {$this->getTable('example')} (
  `example_id` int(11) unsigned NOT NULL auto_increment,
  `fname` varchar(255) NOT NULL default '',
  `lname` varchar(255) NOT NULL default '',
  `email` text NOT NULL default '',
  PRIMARY KEY (`example_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
");
$installer->endSetup();
?>

=> app\code\local\Xyz\Example\sql\example_setup\mysql4-upgrade-0.1.0-0.1.1.php

<?php
$installer = $this;  //Getting Installer Class Object In A Variable
$installer->startSetup();
$installer->getConnection()->addColumn(
    $installer->getTable('example'), 'date', "date AFTER `email`");

$installer->endSetup();
?>

Best Answer

You will need a resource model with a method that returns true if the customer email is already registered.

Thankfully, this is already implemented both for customers and for email signups:

Newsletter Signup:

$subscriber = Mage::getModel('newsletter/subscriber')->loadByEmail($email);
if($subscriber->getId()){
//your logic
}

Customer Registration:

$customer = Mage::getModel('customer/customer')->loadByEmail($email);
if($customer->getId()){
//your logic
}

See how this is implemented in a resource model below:

public function loadByEmail(Mage_Customer_Model_Customer $customer, $email, $testOnly = false)
{
    $adapter = $this->_getReadAdapter();
    $bind    = array('customer_email' => $email);
    $select  = $adapter->select()
        ->from($this->getEntityTable(), array($this->getEntityIdField()))
        ->where('email = :customer_email');

    //... removed some website scope stuff for brevity

    $customerId = $adapter->fetchOne($select, $bind);
    if ($customerId) {
        $this->load($customer, $customerId);
    } else {
        $customer->setData(array());
    }

    return $this;
}

As you can see, the heavy lifting is in the $adapter->select() which is limited to the where clause that only loads customers by a singular email address. Then fetchOne is called, which, if it returns anything at all then loads a customer.

In your case, a simple return true would alert you to whether the email exists or not. What you do after that is up to the specific requirements of your task and application.