AJAX Load Content No Page Refresh |
02-18-2017, 06:23 AM
(This post was last modified: 02-18-2017, 09:12 AM by ciadmin. Edit Reason: Added bbcode for readability )
Hi Friends this is my Simple Header Page with few link to call different pages.
Whenever i click on any link it refresh whole page then display the page content. I want to do it in ajax way i.e, without page refresh. I am new in ajax use. Here are my code please help me to acheive it. header.php ---------- Code: <DOCTYPE html> one of my controller called category is like this: -------------------------------------------------- Code: <?php My view_file is category_view. Please note that the code works but i want to avoid page refresh. Thanks in Advance for any help in this regard.
Very simple in this link.
http://api.jquery.com/jquery.ajax/ Replace url with codeigniter url and define post vars.
Practical guide to IgnitedCMS - Book coming soon, www.ignitedcms.com
(02-19-2017, 08:33 AM)ignitedcms Wrote: Very simple in this link. what do you mean by "and define post vars. " I understand that in place of url it will be my base_url() Please give me a code as i am not using ajax must. Shall be greatful . Really Urgent Thanks
If we give every thing to you, you will never learn.
What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
The basic idea is to make take your anchor link and send the URL through $.ajax, return the response (view) as a key in a JSON blob, then replace the HTML in a container with your new fragment. This assumes your initial page load has all the required assets (CSS, JS) and you'll have to bind any plugins you use manually after you've replaced the content.
One of these days I'll update my Cinder repo which does all of this for you. Our internal build is stable but I haven't had a chance to back port the changes to the generic version.
Sounds pretty complex, but it isn't.
The whole idea is that you have an html element on your page, let's say a <div>, that must be populated with a different content, triggered by some user action. For a start, you'll have to give this html element a unique id, in order to change it's content with jQuery. Code: <div id="mycontent"><p>Old piece of content blablabla<p/></div> Next, you write a method (= function) inside a controller that must return the new content. Make sure it ends with an echo statement: PHP Code: echo '<p>New piece of content tadaaa!</p>'; If this method must accept one or more parameters, the safest way is passing 'POST' variables to it in the AJAX request. Handle them with the standard CI way for $_POST variables, e.g.: PHP Code: $category = $this->input->post('category'); The last step is the jQuery part in your view. Make sure your view has a link to the jQuery library in the <head> section. PHP Code: <script> After the user has clicked the button, the div will have the new content without a page refresh. (02-22-2017, 02:43 PM)Thanks a lot for your guide Wouter60 Wrote: Sounds pretty complex, but it isn't.
jQuery.load() is probably the easiest way to load data asynchronously using a selector, but you can also use any of the jquery ajax methods (get, post, getJSON, ajax, etc.)
Note that load allows you to use a selector to specify what piece of the loaded script you want to load, as in
Code: $("#mydiv").load(location.href + " #mydiv"); Note that this technically does load the whole page and jquery removes everything but what you have selected, but that's all done internally.
You do know that this post is 3 years old.
What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
|
Welcome Guest, Not a member yet? Register Sign In |