ajax - Cakephp : add another view page link on the view page -
sorry asking question ..i working on cakephp 2.x ... have view page in controller name folder e.g controller/index.ctp ... , have ajaxfiles stored in app/webroot/ajax/ajaxfile.html
now on index.php file acessing ajax page
<a href="ajax-demo/ajaxfile.html" class="file-link"> <span class="icon file-png"></span> simple gallery</a>
controller
public function index(){ }
now problem want send variables both of pages ... index.ctp , ajaxfile ... how can ??what best approach tackle these things ....
do have move ajaxfiles webroot , paste under controller name folder? if how can send variables ajax files has no model , controller
please if 1 know solution please advice me. , give example
there different way achieve this, here i'm writing simplest one
first need move "index.ctp" file "view/your controller name/" folder.
1) access variable in view need set controller's method this
public index(){ $this->set('yourvariable', 'your value'); }
2) access value in view file (index.ctp), need call variable this
$yourvariable;//if want print can write echo $yourvariable;
3) call ajax file index.ctp simplest method call onclick event on anchor, onclick event call javascript method further make ajax call , place output in element in index.ctp, ajax call further call controller method (implement html related logic here)
for example,
<a href="#" class="file-link" onclick="yourajaxcallmethod('http://'.<?php echo $_server['http_host'].$this->webroot;?>.'yourcontroller/ajaxmethod/'.<?php echo $yourvariable;?>)"><span class="icon file-png"></span>simple gallery</a> <div id="yourajaxfileoutputreplacementdiv"></div>
4) create javascript method in js file, js file must loaded in layout file.
function yourajaxcallmethod(baseurl,yourvarible) { //initialize ajax method var req = getxmlhttp();//let's method initialize ajax if (req) { req.onreadystatechange = function() { if (req.readystate == 4) { if (req.status == 200) { document.getelementbyid('yourajaxfileoutputreplacementdiv').innerhtml=req.responsetext; } else { alert("there problem while using xmlhttp:\n" + req.statustext); } } } var url = baseurl+yourvarible+'/'+math.random(); req.open("get", url, true); req.send(null); } }
5) ajax file related method in controller "yourcontroller". set autorender false
public function ajaxmethod(){ $this->autorender = false; //check $this->request['pass'] arguments send ajax call $retreivedvariable = $this->request['pass'][0]; echo 'i retrieved variable'.$retreivedvariable; }
however instead of writing core javascript , ajax method can call inbuild ajax helper same.
Comments
Post a Comment