Belove是我的代码,我正在尝试将输出从控制器传递到视图,现在我的问题是当我对代码$errormsg = $this->_setError('f_001'); $this->_view->set('errormsg', $errormsg);
进行编码时,它在index()中工作,但不在processjobsearch()中。
processjobsearch()函数我通过ajax调用,如何设置输出现在查看,我哪里出错了?请任何人帮助我..
**在jobs / 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 :(得分:0)
_setError()
或Controller
中未定义JobsController
方法。它不应该使用index()
或processjobsearch()
方法。
我认为你想做这样的事情(尽管我不确定为什么):
class Controller {
//...
protected function _setError($errorCode) {
//do something with code to produce a message??
return $errorMsg;
}
}
编辑:
好吧,我明白你现在在做什么。$errormsg
方法中的变量jobs/index.tpl
由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);
}
});
}