0

I have an index.php file. My wish is that, when clicking on a button (which is in index.php) an AJAX request should load about.php and include it inside a div tag in index.php.

Below is my code. But this is not working:

index.php:

<a href=javascript:onClick('about.php')>About</a>
<div id='dynamo'>
   <?php 
       if($_POST){
       $page=$_POST['page'];                   
       include $page;
       }
   ?>
</div>

PageSelect.js

 function onClick(text){       
 $.ajax({
     type:"POST",
     url:"index.php",
     datastring:'page='+text          
  });     
 }
2
  • Make sure that 1. you have included Jquery lib in your script. 2. Also check you have given correct name along with file path in url inside ajax request. Commented Aug 31, 2015 at 12:10
  • yes i have checked again, jquery lib is properly referenced Commented Aug 31, 2015 at 12:36

1 Answer 1

1

You need something like this (with Jquery):

HTML:

<a href="#" id="click">About</a>
<div id="render"></div>

JS:

$(document).ready(function () {

    $('#click').click(function () {
        $.ajax({
            url: "about.php",
            success: function (response) {
                $('#render').html(response);
            }
        });
    });

});

JSFiddle

Sign up to request clarification or add additional context in comments.

13 Comments

i used your solution, but the problem is that, when i click the about button the function does not get fired. I even put a breakpoint over the $('#click').click(function () line and clicked the about again. But the debugger did not hit the breakpoint. i double checked the id name under a tag and one that is used in jquery, but in vain... @Michelem
Are you sure to have the about.php file in the right place? Can you see any error on the debug console of your browser?
Sorry I missed a # in render it should be $('#render').html(response);
Yeah about.php is in root directory only. Even then the problem is that the click event in jquery is not getting fired upon
Even i added an alert statement inside click event handler. But even that alert is not being fired.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.