2015-06-19 81 views
0

貝洛夫是我的代碼,我想從控制器傳遞輸出來查看,現在我的問題是,當我做代碼這$errormsg = $this->_setError('f_001'); $this->_view->set('errormsg', $errormsg);它工作在索引(),但不是在processjobsearch()。傳遞輸出從控制器查看

processjobsearch()函數我正在通過ajax調用,我該如何設置輸出以查看現在,哪裏出錯?請誰能幫助我..

**調用函數在工作/ index.tpl裏**

function jobsearch() 
    { 
     var form=$("#jobSearchForm") 
      //$("#result").html("<img alt="ajax search" src='ajax-loader.gif'/>"); 
     $.ajax({ 
       type: 'POST', 
       url: '/jobs/processjobsearch/', 
       data: form.serialize(), 
       success: function(data){ 
       alert(data); 
       //document.getElementById("display").innerHTML = data; 
          $.each(data, function(index, value) { alert(value); 
            $.each(value, function(index, value) { 
             $("#data").append("<tr><td>" + value + '</td></tr>'); 
           }); 
          });   
          }  
      }); 
    } 
//accessing value like this 

<?php echo $errormsg; ?> //this gives me output if called from index() 
      //but doesn't give output when called from processjobsearch() 

全球視野

class View 
{ 
    protected $_file; 
    protected $_data = array(); 

    public function __construct($file) 
    { 
     $this->_file = $file; 
    } 
    public function assign($variable , $value) 
     { 
     $this->data[$variable] = $value; 
     } 
    public function set($key, $value) 
    { 
     $this->_data[$key] = $value; 
    } 

    public function get($key) 
    { 
     return $this->_data[$key]; 
    } 

    public function output() 
    { 
     if (!file_exists($this->_file)) 
     { 
      throw new Exception("View " . $this->_file . " doesn't exist."); 
     } 

     extract($this->_data); 
     ob_start(); 
     include($this->_file); 
     $output = ob_get_contents(); 
     ob_end_clean(); 
     echo $output; 
    } 
} 

全局控制器

class Controller 
    { 
     protected $_model; 
     protected $_controller; 
     protected $_action; 
     protected $_view; 
     protected $_modelBaseName; 
     protected $cookName; 
     //protected $_data = array(); 

     public function __construct($model, $action) 
     { 
      $this->_controller = ucwords(__CLASS__); 
      $this->_action = $action; 
      $this->_modelBaseName = $model; 

      $this->_view = new View('views' . DS . strtolower($this->_modelBaseName) . DS . $action . '.tpl'); 
     } 

     protected function _setModel($modelName) 
     { 
      $modelName .= 'Model'; 
      $this->_model = new $modelName(); 
     } 

     protected function _setView($viewName) 
     { 
      $this->_view = new View('views' . DS . strtolower($this->_modelBaseName) . DS . $viewName . '.tpl'); 
     } 
     public function _setError($errorCode) 
     { //echo "pass1"; 
     $errormsg = $this->_model->getError($errorCode); //echo $errormsg [jserr_msg]; 
     return $errormsg [jserr_msg]; 
     } 
} 

jobscontroller

class JobsController extends Controller 
{ 
    public function __construct($model, $action) 
    { 
     parent::__construct($model, $action); 
     $this->_setModel($model); 
    }  
    public function index() 
    { 
     $this->_view->set('title', 'Job Search');   
     $script2 = BASE_FRONT.'js/jquery-1.11.3.min.js';  
     $script4 = BASE_FRONT.'js/scw.js';  
     $js_global = array($script2, $script4); 
     $this->_view->set('js_global', $js_global); 
    $errormsg = $this->_setError('f_001'); 
     $this->_view->set('errormsg', $errormsg); //in index function it works 
     return $this->_view->output(); 
    } 
    /** 
    * Defines processjobsearch, called from jobs/index.tpl file 
    * it process value to search getJobSearchData function in jobsinmodel to get relative detail. 
    */ 
    public function processjobsearch() 
{ 
try { 
    $valToSearch = isset($_POST['keyword']) ? trim($_POST['keyword']) : NULL; 
     $nature = isset($_POST['nature']) ? trim($_POST['nature']) : NULL; 
     $jobs = $this->_model->getJobSearchData($valToSearch, $nature);  
     // print_r($jobs); 
    $errormsg = $this->_setError('f_001'); 
    $this->_view->set('errormsg', $errormsg); //here it doesn't works 
    $this->_view->set('jobs', $jobs); 
    // return $this->_view->output(); 
    } catch (Exception $e) { 
     echo '<h1>Application error:</h1>' . $e->getMessage(); 
    }  
    } 
} 

回答

0

_setError()方法沒有ControllerJobsController定義。它不應該在index()processjobsearch()方法中工作。

我假設你想要做這樣的事情(雖然我不知道爲什麼):

class Controller { 
    //... 

    protected function _setError($errorCode) { 
     //do something with code to produce a message?? 
     return $errorMsg; 
    } 
} 

編輯:

沒關係啊,我看你現在在做什麼。 jobs/index.tpl中的變量$errormsg由您的View::output()方法完成。顯然JS(作爲客戶端)不能更新這個PHP變量,所以你需要通過AJAX獲取值並填充包含這個值的HTML元素。

我可以看到你的JS代碼試圖用processjobsearch()(通過AJAX)的結果填充表格單元格,但是實際上這個方法沒有返回任何結果。您將數據添加到視圖中,但不會輸出它。

也許,而不是jobs/processjobsearch.tpl,您可以使用jobs/processjobsearch.json並使用您的視圖數據填充它。然後,您可以通過ajax獲取這些數據,並使用JS來更改錯誤消息。

事情是這樣的:

function jobsearch() 
{ 
    var form=$("#jobSearchForm") 
    //$("#result").html("<img alt="ajax search" src='ajax-loader.gif'/>"); 
    $.ajax({ 
     type: 'POST', 
     url: '/jobs/processjobsearch/', 
     data: form.serialize(), 
     success: function(data){ 
      var json = $.parseJSON(data); 

      $.each(json.jobs, function(index, value) { alert(value); 
       $.each(value, function(index, value) { 
        $("#data").append("<tr><td>" + value + '</td></tr>'); 
       }); 
      }); 

      $('#errorr-msg').html(json.errormsg); 
     } 
    }); 
} 
+0

我已經編輯我的問題我已經忘了補充問題該功能,但它確實存在DER – Durga

+0

'/ processjobsearch.tpl'沒有文件,它是一個功能,所以怎麼能我將它更改爲'/ processjobsearch.json',並且我嘗試了你的代碼,它給了我這個錯誤'JSON.parse:意外的字符在JSON數據的第1行第1列' – Durga

+0

我知道'processjobsearch.tpl' doesn'實際上還存在,但我假設了一個基本的命名約定。也就是說,如果'JobController :: index()'有模板'jobs/index.tpl','JobController :: processjobsearch()'的模板將是'jobs/processjobsearch.tpl'。我建議通過AJAX調用'processjobsearch()'方法,結果應該是JSON。然後你可以用JS分離出作業數據和錯誤數據。上面的代碼不應該只是工作,而是讓你走上正確的軌道。 – PiranhaGeorge

相關問題