2010-01-31 89 views
0

我目前使用zend框架和使用jquery和php。嘗試實現自動檢查用戶名可用性。使用zend框架調用函數

對於下面的代碼,我需要調用我的user_availability.php這是在我的控制器文件夾。我做了/ controllers/user_availability,../user_availability,但它沒有調用該頁面。誰能幫忙?

$.post("user_availability.php",{ user_name:$(this).val() } ,function(data) 
+0

您能否提供其他信息。 user_availability.php結構如何?也許你可以發佈該文件的內容。 – user250120 2010-02-07 20:25:11

回答

0

,如果你忠實地想打電話與Zend框架的純PHP文件,那麼你需要把你的公共文件夾內,並把它作爲/user_availability.php,但幾乎可以肯定應該是更好地使用戶可用性您的控制器之一

+0

所以,如果讓我說我有一個checkcontroller.php位於我的控制器和我的useravailability是在該頁面的功能。我怎麼稱呼它? $。員額( 「checkcontroller.php」,{USER_NAME:$(本).VAL()},功能(數據) 因爲這不工作或者.. – Ryan 2010-01-31 15:22:13

+0

你可以發佈你的控制器的代碼段, ?/或您的Zend應用佈局 – roman 2010-01-31 16:27:20

+0

類UsercheckController擴展化Zend_Controller_Action { \t \t \t公共職能的indexAction(){\t \t \t \t $查看=合適的詞彙::得到( '查看'); \t \t \t $視圖 - > render('register2.tpl'); \t \t $ users = new Users(); \t \t $ username = $ _POST ['username']; \t \t require_once'Zend/Db.php'; \t \t $ DB2 = Zend_Db的::工廠( 'PDO_MYSQL',陣列( \t \t \t '主機'=> '127.0.0.1', \t \t \t '用戶名'=> 'roseadmin', \t \t \t「密碼」 => 'test123', \t \t \t 'DBNAME'=> 'roseuser', \t \t \t '端口'=> '3306' \t \t)); \t \t \t 如果\t($用戶 - > checkUnique($ _ POST [ '用戶名'])){ \t \t回聲 「失敗」; \t \t \t \t \t \t \t \t \t } \t } } – Ryan 2010-02-01 03:31:38

0

查找到行動和視圖助手(取決於你想調用從腳本)的作用。動作助手可以從控制器調用,並可以保存在... /項目/庫中。

從你可以調用你寫,並存儲在... /項目名稱/應用/視圖/傭工自定義視圖助手視圖。

您可能需要調整你的代碼,以適應框架內更好,但只是創建需要一個幫手(包括)的.PHP腳本應該工作。

View helpers (Survive the Deep End)

Action helpers (from the manual):

+0

我不太明白。我試圖調用控制器的代碼位於視圖文件夾中。 – Ryan 2010-02-01 03:39:57

0

看來你正在做一個頁面請求到非控制器項目。無論何時,當您在Zend Framework中發出請求時,它都會嘗試將請求的URL與系統中的路由相匹配,這與控制器操作相對應。所有這些都是通過您的應用程序的index.php文件處理的 - 直接提供其他php文件的唯一方法是通過公用文件夾。

我的建議是創建檢查一個Ajax請求,並在你的選擇(XML,JSON)的格式返回的東西(而不是渲染HTML模板)的控制器操作。

public function userAvailableAction() 
{ 
    if ($this->getRequest()->isXmlHttpRequest()) { 
     // check user availability 
     $this->_helper->json($data); 
    } 

}

+0

我確實創建了控制器操作。但它只是不叫它。我從視圖文件夾調用,該模板使用.tpl。這很重要嗎? – Ryan 2010-02-01 03:43:23

0

謝謝大家。我找到了解決辦法。

你只需要把功能的控制器頁面並調用該函數的名稱。

e.g:

$.post("check",{ user_name:$(this).val() } ,function(data) 

然後在你的控制器文件checkusername可用性確保您添加檢查功能。

0

我的控制器頁面如下代碼:

class RegisterController extends Zend_Controller_Action { 
    public function checkAction(){ 
     $users = new Users(); 
     $username = $_POST['username']; 
     if($users->checkUnique($_POST['username'])){ 
      echo "fail"; 
     }   
} 

在這種情況下,checkUnique只是一個SQL語句在我的模型控制器檢查,如果用戶名存在。

對於我的jQuery代碼是:

$("#username").blur(function(){ 
    //remove all the class add the messagebox classes and start fading 
    $("#msgbox").removeClass().addClass('messagebox').text('Checking...').fadeIn("slow"); 
    //check the username exists or not from ajax 
    $.post("check",{ user_name:$(this).val() } ,function(data){ 
     if(data=='no'){ //if username not avaiable 
      $("#msgbox").fadeTo(200,0.1,function(){ //start fading the messagebox 
       //add message and change the class of the box and start fading 
       $(this).html('This User name Already exists').addClass('messageboxerror').fadeTo(900,1); 
      }); 
     }else { 
     $("#msgbox").fadeTo(200,0.1,function(){ //start fading the messagebox 
       //add message and change the class of the box and start fading 
       $(this).html('Username available to register').addClass('messageboxok').fadeTo(900,1); 
      }); 
     } 
    }); 
}); 

我從這個鏈接下面的例子:

http://roshanbh.com.np/2008/04/check-username-available-ajax-php-jquery.html。請看看它。我希望它有幫助。 =)