Check customer email is already exist in magento using ajax prototype

Check customer email is already exist in magento using ajax prototype

August 9, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Check customer email is already exist in magento using ajax prototype

Checking Customer Email Existence in Magento using AJAX Prototype

Magento is a feature-rich e-commerce platform that provides extensive functionality out of the box, including customer management features. One common requirement is to check if a customer email already exists before allowing registration or update operations. This can be achieved using AJAX prototype along with some custom controller logic.

The Problem at Hand

You’ve shared your custom code for checking if an email already exists in the Magento customer database. While the approach seems correct, you’re facing issues where the PHP function is returning full page HTML instead of a simple JSON response indicating whether the email is available or not.

Let’s dive into the potential issues and provide a solution.

Understanding the Issue

  1. Action Request Type: You’ve configured your action to be processed asynchronously using asynchronous: false. This means the script will wait for the AJAX request to complete before continuing with other JavaScript execution. However, in Magento, this might interfere with other asynchronous processes running on the page.

  2. Response Parsing: In your success callback, you’re using eval('(' + transport.responseText + ')') to parse the JSON response. While this works in some cases, it’s generally not recommended due to security risks associated with evaluating arbitrary code from a remote server.

  3. Controller Response Format: Your controller is returning a JSON response, which should be handled correctly by Prototype.js. However, if there are any issues with the content type header or the response body format, it might result in the full page HTML being rendered instead of the expected JSON data.

The Solution

To resolve this issue, we need to ensure that our controller is returning a clean JSON response and that the AJAX request is handling it correctly. Here’s an updated approach:

  1. Update the Controller Action:

    Update your checkEmailAction in the CustomerController.php file as follows:

    public function checkEmailAction()
    {
        $bool = Mage::getModel('customer/customer')->setWebsiteId(Mage::app()->getWebsite()->getId())
            ->loadByEmail($this->getRequest()->getParam('email'))
            ->getId() ? 1 : 0;
    
        $jsonStatus = 200;
        $info = array("status" => $bool);
    
        $this->getResponse()
             ->setBody(json_encode($info))
             ->setHttpResponseCode($jsonStatus)
             ->setHeader('Content-type', 'application/json', true);
    
        return $this;
    }
    

    This version of the function uses a more concise approach to check if a customer with the given email exists in the specified website context.

  2. Update the AJAX Request:

    Update your JavaScript code as follows:

    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);
        $.ajax({
            url: url,
            type: "GET",
            success: function(response) {
                try {
                    var obj = JSON.parse(response);
                    if (obj.status === 1) {
                        Validation.get('validate-email').error = validateTrueEmailMsg;
                        ok = false;
                    } else {
                        ok = true; /* return true or false */
                    }
                } catch(e) {}
            },
            error: function() { alert('something wrong') },
        });
        return ok;
    });
    

    Here, we’re using jQuery’s $.ajax method, which handles JSON responses automatically. The JSON.parse method is used to parse the response text as a JSON object.

  3. Ensure Proper Error Handling:

    In your success callback, wrap the JSON.parse call in a try-catch block to handle any parsing errors gracefully.

Conclusion

Checking if a customer email already exists in Magento using AJAX and Prototype.js requires careful handling of both the server-side response and client-side request. By updating the controller action to return a clean JSON response and updating the JavaScript code to use jQuery’s $.ajax method for simplicity, you should be able to resolve the issue.

Remember, when dealing with asynchronous requests in Magento, it’s essential to ensure that your custom JavaScript does not interfere with other native JavaScript processes running on the page.

For more detailed insights into Magento development and best practices, I recommend checking out our blog at Laravel Company. We cover a wide range of topics related to e-commerce development using Laravel and Magento.

Database Security Configuration Email

Our network