Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    May 2013
    Posts
    13
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Loading PHP file into DIV via AJAX

    Hi everyone,

    This is another n00b question but here goes...

    Say I have an index.php that loads a page.php into one of its divs. So:

    $('#result').load('page.php');

    page.php in turn has its own links and I want it so that when you click on one of these links it loads the respective page into the SAME #result div. Similarly that loaded page might have its own links and I want to continue loading content into the same div. How might I achieve this chain effect of pages?

    Thank you for your time.

  • #2
    Regular Coder patryk's Avatar
    Join Date
    Oct 2012
    Location
    /dev/couch
    Posts
    398
    Thanks
    2
    Thanked 64 Times in 64 Posts
    that's javascript question. what is it in php forum?

    you would use callback function for .load()
    Code:
    //function you'll be using
    function loadPage(url){
    	$('#result').load(url, function(){
    		$('#result').find($('a')).each(function(){
    			$(this).on('click', function(e){
    				loadPage($(this).attr('href'));
    				e.preventDefault();
    			});
    		});
    	});
    }
    //when document loads, run function first time loading initial page
    $(document).ready(function(){
    	loadPage('page.php');
    });
    Last edited by patryk; 05-05-2013 at 12:18 PM.

    -------------------------------------------------------------------------------
    "Real Programmers can write assembly code in any language" - Larry Wall

  • Users who have thanked patryk for this post:

    pintee (05-07-2013)


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •