如何使用magento 2中的ajax检查电子邮件已存在

问题描述 投票:0回答:2

控制器Email.PHP`

public function execute()
    {
        $customerEmail=$this->getRequest()->getParam('email');
        $objectManager=\Magento\Framework\App\ObjectManager::getInstance();
        $CustomerModel = $objectManager->create('Magento\Customer\Model\Customer');
        $CustomerModel->setWebsiteId(1);

        $CustomerModel->loadByEmail($customerEmail);
        $userId = $CustomerModel->getId();
        if ($userId) {
            return 1;
        } else {
            return 0;
        }
    }`

jQuery的

jQuery(function() {
        var emailAddress = jQuery('#email_address');
        emailAddress.on("change", function () {
           var mail=emailAddress.val();

            jQuery.ajax({
                type: "POST",
                url: "/customer/email/",
                dataType: "json",
                data: {email: mail},
                success: function (exist) {
                    if (exist == 1) {
                       alert("exist");
                    } else if (exist == 0) {
                        alert("exist");
                    }
                },
                error: function (jqXHR, textStatus, errorThrown) {

                        alert("Error " + jqXHR.status + " " + jqXHR.statusText);


                }
            });
        });
    });

我想在使用Ajax点击创建帐户按钮之前检查电子邮件,我没有这样做,请帮我解决这个问题,提前谢谢。

magento2.2
2个回答
0
投票

简单地调用这样的ajax

        var mail = '[email protected]';
jQuery.ajax({
    type: "POST",
    url: "/module/checkemail/",
    dataType: "json",
    data: {email: mail},
    success: function (exist) {
        if (exist == 1) {
            // js for email exists
        } else if (exist == 0) {
            // js for not
        }
    },
    error: function (jqXHR, textStatus, errorThrown) {
                   // error handling
    }
});

比制作​​一个控制器并通过电子邮件加载客户

$CustomerModel = $objectManager->create('Magento\Customer\Model\Customer');
$CustomerModel->setWebsiteId(1); **//Here 1 means Store ID**
$CustomerModel->loadByEmail($customerEmail);
$userId = $CustomerModel->getId();
if ($userId) {
    return 1;
} else {
    return 0;
}

i

如果你得到真实而不是电子邮件存在


0
投票

当客户输入他的电子邮件地址时,您似乎正在尝试验证电子邮件地址。为此,您只需在电子邮件地址字段中进行细微更改即可。

<input type="email" name="email" id="email_address" autocomplete="off" value="<?php echo $block->escapeHtml($block->getFormData()->getEmail()) ?>" title="<?php /* @escapeNotVerified */ echo __('Email') ?>" class="input-text" data-validate="{required:true, 'validate-email':true, 'remote':'<?php echo $this->getUrl('customcustomer/index/uniqueemail', ['_secure' => true]); ?>'}"/>

创建一个控制器并在execute方法中添加逻辑。

<?php 
namespace Gaurav\CustomCustomer\Controller\Index;

use Magento\Framework\App\Action\Action;

class Uniqueemail extends Action
{
 /**
 * @var \Magento\Framework\Controller\Result\JsonFactory
 */
 protected $resultJsonFactory;

 /**
 * @var \Magento\Customer\Model\Customer 
 */
 protected $_customerModel;

 /**
 * @param \Magento\Framework\App\Action\Context $context
 * @param \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory
 */
 public function __construct(
     \Magento\Framework\App\Action\Context $context,
     \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory,
     \Magento\Customer\Model\Customer $customerModel
 ) {
     $this->resultJsonFactory = $resultJsonFactory;
     $this->_customerModel = $customerModel;
     parent::__construct($context);
 }

 public function execute()
 {
     $resultJson = $this->resultJsonFactory->create();
     $email = $this->getRequest()->getParam('email');
     $customerData = $this->_customerModel->getCollection()
                   ->addFieldToFilter('email', $email);
     if(!count($customerData)) {
       $resultJson->setData('true');
     } else {
       $resultJson->setData('That email is already taken, try another one');
     }
     return $resultJson;
 }
}

我希望这对你有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.