2013-10-03 53 views
1

我試圖避免離線的人在已註冊爲客戶的firecheckout頁面上輸入電子郵件。因此,我在我的一個開發控制器中創建了這個示例php腳本:Magento用jQuery通過電子郵件檢查客戶是否已經存在

public function mailExists(Varien_Object $customer, $mail, $websiteId) 
{ 
    if($websiteId){ 
     $customer->setWebsiteId($websiteId); 
    } 
    $customer->loadByEmail($mail); 
    if($customer->getId()){ 
     return $customer->getId(); 
    } 
    return FALSE; 
} 

public function formAction() 
{ 
    $customer = Mage::getModel('customer/customer'); 
    $websiteId = Mage::app()->getWebsite()->getId(); 
    $email = $_POST['email']; 
    if($this->mailExists($customer, $email, $websiteId)) 
    { 
     echo 'mail <u>'.$email.'</u> exists containing customer id '.$this->mailExists($customer, $email, $websiteId); 
    } 
    else{ 
     $this->_formPost(); 
    } 
} 

public function _formPost() 
{ 
    echo "<form enctype='multipart/form-data' action='".Mage::getUrl('index/form/')."' method='post'>"; 
    echo "<input type='text' value='[email protected]' id='email' name='email' />"; 
    echo "<input type='submit' value='verzend' />"; 
    echo "</form>"; 
} 

它工作得很好。但我擔心的是,當某人在結賬頁面輸入所有字段並按下提交按鈕時,所有輸入的數據都將被設置爲空。這會給我的客戶帶來挫敗感。所以我認爲jQuery腳本會更好地通知離線客戶不要使用已經註冊的電子郵件,或者當他們在電子郵件地址輸入字段中輸入電子郵件地址時登錄他們的賬戶。

我想讓我的jQuery腳本運行在用戶輸入的文本上的電子郵件輸入字段。 我只是想出了這一點:

public function testAction() 
{ 
    echo(" 
      <script src='http://code.jquery.com/jquery-latest.js'></script> 
      <script type='text/javascript'> 
      $(document).ready(function() 
      { 
       $('#email').onChange(function() 
       { 
        alert('input'); 
       }); 
      }); 
      </script> 
     "); 
    $this->_testPost(); 
} 

public function _testPost() 
{ 
    echo "<form name='test' enctype='multipart/form-data' action='".Mage::getUrl('index/form/')."' method='post'>"; 
    echo "<input type='text' value='' id='email' name='email' />"; 
    echo "<input type='submit' value='verzend' />"; 
    echo "</form>"; 
} 

這將打印input的警告框。我將如何在jQuery中實現我的php檢查? 由於提前, 巴蒂爾

回答

1

我會做什麼使用jQuery的AJAX調用打電話給你的控制器功能,我會改寫更像如下:

public function ajaxAction() 
{ 
    $customer = Mage::getModel('customer/customer'); 
    $websiteId = Mage::app()->getWebsite()->getId(); 

    if (array_key_exists('email', $_POST)) { 
     $email = $_POST['email']; 
    } else { 
     $this->getResponse()->setBody(false); 
     return; 
    } 
    if ($websiteId) { 
     $customer->setWebsiteId($websiteId); 
    } 
    $customer->loadByEmail($email); 
    if ($customer->getId()) { 
     $this->getResponse()->setBody(true); 
     return; 
    } 
    $this->getResponse()->setBody(false); 
    return; 
} 

所以真的要移動所有您的JavaScript您的控制器和你的觀點,然後用jQuery.ajax方法做這樣的事情:

var mail = '[email protected]'; 
jQuery.ajax({ 
    type: "POST", 
    url: "<?php echo Mage::getUrl('your/frontend/ajax') ?>", 
    dataType: "json", 
    data: {email: mail}, 
    success: function (exists) { 
     if (exists == 0) { 
      // js for fail, hide disable button etc etc 
     } else if (exists == 1) { 
      // js for success bits 
     } 
    }, 
    error: function (jqXHR, textStatus, errorThrown) { 
        // error handling 
    } 
}); 

與AJAX美是你可以解僱檢查,當你想,這樣你就可以聽要求電子郵件完整,並在他們輸入詳細信息後儘快提出請求。

0

你不需要ajax。如果用戶在onepage checkout登錄頁面被重新加載,那麼模板中的一個簡單的javascript應該可以工作。 你可以直接在頁面加載時進行檢查,只在需要時添加javascript。
例如:

<?php if({your check for the email}):?> 
<script type='text/javascript'> 
    $(document).ready(function() 
    { 
     $('#email').onChange(function() 
     { 
      alert('input'); 
     }); 
    }); 
</script> 
<?php endif;?> 

或者如果你想使用原型代替的jQuery:

<?php if({your check for the email}):?> 
<script type='text/javascript'> 
    $(document).observe('dom:loaded', function(){   
     $('#email').observe('change', function(){    
      alert('input'); 
     }); 
    }); 
</script> 
<?php endif;?> 
+0

謝謝你回答我的問題!儘管如此,我發現了一個不同的解決方案。類似於@ @ input所寫的內容。 –

相關問題