php - pass output from controller to view -
belove code , trying pass output controller view, problem when code $errormsg = $this->_seterror('f_001'); $this->_view->set('errormsg', $errormsg);it workes in index() not in processjobsearch().
processjobsearch()function calling through ajax ,how can set output view , going wrong ? pls can me out ..
**calling function in 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 <?php echo $errormsg; ?> //this gives me output if called index() //but doesn't give output when called processjobsearch() global view
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; } } global controller
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 works return $this->_view->output(); } /** * defines processjobsearch, called jobs/index.tpl file * process value search getjobsearchdata function in jobsinmodel 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 doesn't works $this->_view->set('jobs', $jobs); // return $this->_view->output(); } catch (exception $e) { echo '<h1>application error:</h1>' . $e->getmessage(); } } }
the _seterror() method not defined in controller or jobscontroller. shouldn't work in either index() or processjobsearch() methods.
i assume want (although i'm not sure why):
class controller { //... protected function _seterror($errorcode) { //do code produce message?? return $errormsg; } } edit:
ah okay, see you're doing now. variable $errormsg in jobs/index.tpl fulfilled view::output() method. js (being client side) cannot update php variable, need grab value via ajax , populate html element containing value.
i can see js code trying populate table cell with result of processjobsearch() (via ajax), nothing returned method. you're adding data view, not outputting it.
perhaps, rather jobs/processjobsearch.tpl, use jobs/processjobsearch.json , populate view data. grab data via ajax , use js change error message.
something this:
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); } }); }
Comments
Post a Comment