ajax update a div |
[eluser]gordon Stewart[/eluser]
Hello, I'm converting an existing site to use Codeigniter. its going slowly - but getting faster & faster as I get used to CI. ![]() I've just got a few (or 1) problem... Existing script :- I'm using Ajax, to auto-load a particular div entry in my HTML.. it basically opens up a php script, Looks through a file, & randomly rotates text from a plain-text file. & displays it on the screen. QUESTION :- How do we do this in Codeigniter ? are there any tutorials, that show us how to use divs & auto-reload. Is it a HELPER or a MODEL ? & how do we auto-load a helper/model ? (Its pretty easy to auto-refresh a CONTROLLER (just refresh the URL..) Is this possible ? EDIT: I mean the div area auto-reloads. Every 40 seconds - The text in that part of the screen changes, as the php script reloads (but the rest of the screen remains static / Does not reload)
[eluser]Mr. Pickle[/eluser]
As far as I can think of you just make the div and let the Ajax script call a certain action within a controller. The fact that it is reloaded every x seconds is just front-end Ajax configuration. Just fix an url like /response/randomtext that answers to Ajax.
[eluser]gordon Stewart[/eluser]
[quote author="Mr. Pickle" date="1273866014"]As far as I can think of you just make the div and let the Ajax script call a certain action within a controller. The fact that it is reloaded every x seconds is just front-end Ajax configuration. Just fix an url like /response/randomtext that answers to Ajax.[/quote] Thanks - Yes, I'm still a beginner with Ajax (this is/was my first Ajax script. I've found 1-2 sites that say Ajax needs to be installed in the server - Is that right ? (I'm just a user, not an admin of the site) I've found this :- http://www.ibm.com/developerworks/web/li...deigniter/ Lots of code (good) - but also confusing (for 2:45 a.m.). I'll spend the weekend comparing my existing Ajax with whats on the page & just switch 'onkeyup' with the timeout function. Should be a good learning curve..... ( I could not find any examples - so far, That just auto-load a php script) - They all use/activate Javascript functions ) off bed soon, i'll come back tomorow & try a few things...
[eluser]Mr. Pickle[/eluser]
Yes you have to include for example jquery in your head section. You can load this directly from a CDN like: http://ajax.googleapis.com/ajax/libs/jqu...ery.min.js And for Ajax, have a look at this crystal clear screencast: Screencast CodeIngniter Ajax Form It takes you through the complete process of including jQuery and having a section refreshed without refreshing the page. Your case is slightly different but you should be able to figure it out ![]() Also take a look at the other screencasts if your interested, they are interesting for beginners!
[eluser]gordon Stewart[/eluser]
Hmmmm Ok, I've fixed other areas & focussing on Ajax now.. Can anyone advise where they put the Ajax php script (that produces the report / output I want displayed).. Do you put it as a helper / controller / where ? MY JAVASCRIPT / AJAX (extract) --------------------------- function Show_Message() { // alert ("1 - Your browser does support XMLHTTP!"); xmlhttp=GetXmlHttpObject(); if (xmlhttp==null) { alert ("Your browser does not support XMLHTTP!"); return; } // alert ("Your browser does support XMLHTTP!"); var url="http://"+document.domain+"/extra/headermessage.php"; xmlhttp.onreadystatechange=stateChanged; xmlhttp.open("GET",url,true); xmlhttp.send(null); setTimeout("Show_Message()",20000); } --------------------------- I tried to create an 'extra' directory, But still doesn't work.. - i included it as a helper / controller etc.. RESULT :- The script IS being valled - as I see the words where i want them.. HOWEVER, it is displaying my header template TWICE, (I think it can't find the JS file, so re-displays the header file & then the 2nd time, does find the JS file..) My directory structure (right now) :- /home/sites/DOMAIN DIRECTORY/application/controllers /home/sites/DOMAIN DIRECTORY/js (when i go http://www.domain.com/js/javafile.js - I can see the javascript file ) Is this correct ? Once I fix the right directory structure & CI finds myAjax the 1st time, the dupe headers will go away. Thanks...
[eluser]Mr. Pickle[/eluser]
Where is your code that calls the js file? Also you should not output directly from your controller. Use a view instead. For example, the ajax script calls: [full_domain]/ajaxresponse/message/ So 'ajaxresponse' is your controller and 'message' is the function within that controller. You can have it supported with post vars or whatever, or even use a model within the controller if necessary. The output to the Ajax script should be handled by a view loaded from the message function. (of course this is a simplified example)
[eluser]gordon Stewart[/eluser]
The 'header' file is in a view.. it just has this :- header.tpl [removed][removed] the /js/ directory is at same level as /home/sites/server_name/application /home/sites/server_name/js Is that right ? (I can see the JS file by going to http://domain.com/js/javafile.js etc ) Ps, I've been advised / told to use CI with Smarty - by website admins - tpl file... EDIT :- Once i get it "going" - I'll move all output to the views... (i've spent 99% of my time on this, on debugging CI / Googling, & 1% actual programming)...
[eluser]Mr. Pickle[/eluser]
header.tpl? Do you use Smarty template or different templating system? By default all CI views are .php extension
[eluser]gordon Stewart[/eluser]
Using CI with Smarty - Smarty templates Yes, I know CI uses PHP templates... --- In the controller / welcome.php script, i have :- #$smarty->assign ("data", $data); #$this->smarty->assign('data', $data); $this->smarty_parser->parse("ci:news.tpl", $data); #$this->load->view('news.tpl', $data); #$this->smarty_parser->parse("news.tpl", $data); if the BOTTOM one is un-commented, It displays the template HTML - but no data.... If any of the others are un-commented (& the others commented out) - It displays the template source-code (sometimes with data being sent to remplate).... (I've got another thread running here)... - Re getting data sent to template... |
Welcome Guest, Not a member yet? Register Sign In |