1

I want to check customer email is already exist or not using ajax prototype. I tried lots of things but it is not working. I write my code like this.

<script type="text/javascript">
        //<![CDATA[
            var dataForm = new VarienForm('form-validate', true);
            Validation.add('validate-emaila', 'Email already exist', function(v) {
    var url = '/customer/account/checkEmail/email?email=' + encodeURIComponent(v);
    var ok = false;
    new Ajax.Request(url, {
        method: 'get',
        asynchronous: false,
        onSuccess: function(transport) {
        alert(transport.responseText);
            var obj = response = eval('(' + transport.responseText + ')');
            validateTrueEmailMsg = obj.status_desc;
            if (obj.ok === false) {
                Validation.get('validate-email').error = validateTrueEmailMsg;
                ok = false;
            } else {
                ok = true; /* return true or false */
            }
        },
        onFailure: function(){ alert('something wrong') },
        onComplete: function() {
            if ($('advice-validate-email-email')) {
                $('advice-validate-email-email').remove();
            }
            if ($('advice-validate-email-email_address')) {
                $('advice-validate-email-email_address').remove();
            }
            if ($('advice-validate-email-billing:email')) {
                $('advice-validate-email-billing:email').remove();
            }
            if ($('advice-validate-email-shipping:email')) {
                $('advice-validate-email-shipping:email').remove();
            }
            if ($('advice-validate-email-_accountemail')) {
                $('advice-validate-email-_accountemail').remove();
            }
        }
    });
    return ok;
});

        //]]>
        </script>

I called a function In accountcontroller

public function checkEmailAction()
    {
      $bool = 0;
      $email = $this->getRequest()->getParam('email');
      $customer = Mage::getModel('customer/customer');
      $customer->loadByEmail($email);
      if ($customer->getId()) {
      $bool = 1;
      }
      $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($bool));


    }

I am getting wrong response from php function. it is returning full html like this. instead of 0 or 1.

<!DOCTYPE html>

<!--[if lt IE 7]><html class="lt-ie9 lt-ie8 lt-ie7" style="background:url(http://103.9.68.227/media/wysiwyg/customBg/customBg.jpg) no-repeat center 0 #fbfbfb;background-attachment:fixed;"><![endif]-->

<!--[if IE 7]><html class="lt-ie9 lt-ie8 style="background:url(http://103.9.68.227/media/wysiwyg/customBg/customBg.jpg) no-repeat center 0 #fbfbfb;background-attachment:fixed;"><![endif]-->

<!--[if IE 8]><html class="lt-ie9 style="background:url(http://103.9.68.227/media/wysiwyg/customBg/customBg.jpg) no-repeat center 0 #fbfbfb;background-attachment:fixed;"><![endif]-->

<!--[if IE 9]><html class="ie9 style="background:url(http://103.9.68.227/media/wysiwyg/customBg/customBg.jpg) no-repeat center 0 #fbfbfb;background-attachment:fixed;"><![endif]-->

<!--[if gt IE 9]><!--><html class="" style="background:url(http://103.9.68.227/media/wysiwyg/customBg/customBg.jpg) no-repeat center 0 #fbfbfb;background-attachment:fixed;"><!--<![endif]-->

    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Customer Login  | Lincraft</title>
<meta name="description" content="" />
<meta name="keywords" content="" />



<link rel="stylesheet" type="text/css" href="http://103.9.68.227/skin/frontend/lincraft/default/css/styles.css" media="all" />

            <div class="page">
                <style>

</style>
<div class="header-container">
    <div class="header-outer">
                <div class="header">
            <div class="branding">
                                    <a href="http://103.9.68.227/" title="Lincraft: where ideas come to life" class="logo"><strong>Lincraft: where ideas come to life</strong><img src="http://103.9.68.227/skin/frontend/lincraft/default/lemonade/img/logo_lincraft-where.png" alt="Lincraft: where ideas come to life" /></a>
                                            </div>


            <div class="header-right ">
                <div class="header-right-info">
                    <div class="welcome-msg"></div>
                    <div class="account-links"><ul class="links">
                        <li class="first" ><a href="http://103.9.68.227/customer/account/login/" title="Login" >Login</a></li>
                                <li class=" last" ><a href="http://103.9.68.227/membership/create/" title="Register" >Register</a></li>
            </ul>
</div>
                </div>
                <div class="header-right-cart">
                    <!--{CART_SIDEBAR_536bebca0e80b1d085b731a445d18480}-->  
<!--
    item 0 show on the front end
-->

<div class="top-cart">

    <div class="block-title">
        <a href="http://103.9.68.227/checkout/cart/" id="topCartTrigger" class="top-cart-number"><strong><span>0</span> items</strong></a>
    </div>

            <!--
            two links that on the header
        -->
        <div class="block-title no-items">
            <ul class="cart-links">
                <li><a href="http://103.9.68.227/checkout/cart/">View Cart</a></li>
                <li><a href="http://103.9.68.227/onestepcheckout/">Check Out</a></li>
            </ul>
        </div>
        <div id="topCartContent" class="block-content" style="display:none">
            <div class="inner-wrapper">                                         <p class="block-subtitle">
                        <span class="close-btn">Close</span>
                        In my cart                  </p>
                    <p class="cart-empty">
                    You have no items in your shopping cart.                    </p>
                                                    </div>
        </div>
</div>
<!--/{CART_SIDEBAR_536bebca0e80b1d085b731a445d18480}-->             </div>
            </div>


        </div>
        <div id="lcNav" class="clearfix">




    </body>
</html>

I have tried lots of thing but giving same response. Can any one tell me what is wrong in this?

2

I would try returning the json response like this:

$status = "PASS"; 
$jsonStatus = 200;

$info =  array( "status" => $status);

$this->getResponse()->setBody(json_encode($info))->setHttpResponseCode($jsonStatus)->setHeader('Content-type', 'application/json', true);

return $this;

If you set the $status to 504 you will return an error.

1
  • Thanks for the response, I have tried your solution. but it still returning full html. When i see ajax response in browser console it show that it is going to the called function but not returning correct value. Any other solution you know to solve this. May 2 '14 at 12:40
1

$this->getResponse()->setBody(Zend_Json::encode($bool));

3
  • Hello, welcome on Magento SE. Please try to elaborate your answer and explain why you think this is the best solution. Jul 27 '15 at 12:01
  • I have used this and its working like charm. I am using Magento 1.9.1
    – Manik
    Jul 27 '15 at 12:15
  • I'm not questioning your answer, I'm just pointing out the fact that it would be more helpful for the OP or others viewing your answer to have a little bit of explanation rather than a one-liner :p Jul 27 '15 at 14:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.