2015-06-19 84 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

相关问题