url in an ajax application - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5) +--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24) +--- Thread: url in an ajax application (/showthread.php?tid=66062) |
url in an ajax application - rooye - 08-30-2016 Hello friends, I am designing an application in which I want to pull data from a php file using javascript technique, namely ajax. In CI am not sure how to access my controller this way. Consider this as my view: <html> <head> <title>An Ajax example</title> <script language = "javascript"> var XMLHttpRequestObject = false; if (window.XMLHttpRequest) { XMLHttpRequestObject = new XMLHttpRequest(); } else if (window.ActiveXObject) { XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP"); } function getData(dataSource, divID) { if(XMLHttpRequestObject) { var obj = document.getElementById(divID); XMLHttpRequestObject.open("GET", dataSource); XMLHttpRequestObject.onreadystatechange = function() { if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200) { obj.innerHTML = XMLHttpRequestObject.responseText; } } XMLHttpRequestObject.send(null); } } </script> </head> <body> <H1>An Ajax example</H1> <form> <input type = "button" value = "Fetch the message" onclick = "getData('data.php', 'targetDiv')"> </form> <div id="targetDiv"> <p>The fetched message will appear here.</p> </div> </body> </html> Thanks for you suggestions RE: url in an ajax application - InsiteFX - 08-30-2016 PHP Code: HTML PHP Code: JS RE: url in an ajax application - PaulD - 08-30-2016 That example looks like it is from ajax for dummies. http://documents.mx/technology/ajax-for-dummies-and-not-only.html A raw ajax call is actually quite complicated, which is why so many frameworks and libraries have popped up to make the whole job easier. For instance in jquery, you would do something like this: PHP Code: $.get('/mycontroller/mymethod', function(theResponse){ And your controller would just do this: PHP Code: <?php defined('BASEPATH') OR exit('No direct script access allowed'); However, in the example of jquery, there is much more you can do with the library: https://api.jquery.com/jQuery.get/ Now you could still load a view as a string (use the third option in the load view and set it to true) for keeping html in views, and you can send data in the url segments or use a post instead of a get. Then you can post an array to the controller, validate it in the usual way, do some database calls, use a view to create a html snippet and return that. The level of complexity you add is up to you. As for your example you posted, you need to give it the url you are sending the request to. You controller just acts in the normal way apart from echoing the results rather than sending them as full page output. I hope that helps, Paul. RE: url in an ajax application - InsiteFX - 08-31-2016 A javascript file can not execute php code only a file with the php extension can execute php code. RE: url in an ajax application - PaulD - 08-31-2016 ? No of course it can't. Not sure where you thought I was saying that but if what I said is not clear then I apologize for any confusion. RE: url in an ajax application - InsiteFX - 08-31-2016 No not at you PaulD, Just stating it so anyone else will not try to do it. |