Sunday, March 6, 2011

How Load a PHP page in Same Window in JQuery

Dear All,

I have Test.php and it has two functions:

   <?php

     echo displayInfo();
     echo displayDetails();

    ?>

Javascript:

<html>
 ...
<script type="text/javascript">

    $.ajax({
      type:'POST',
      url: 'display.php',
      data:'id='+id  ,
      success: function(data){
       $("#response").html(data);
      }
          });     

</script>
...

<div id="response">
</div>
</html>

It returns the response from jQuery.

The response shows as <a href=Another.php?>Link</a>

When I click the Another.php link in [test.php], it loads in another window. But I need it should load same <div> </div> area without changing the content of [test.php], since it has displayInfo(), displayDetails().

Or is it possible to load a PHP page inside <div> </div> elements?

How can I tackle this problem?

Any suggestions?

From stackoverflow
  • If I understand correctly, you'd like for the a link to cancel navigation, but fire the AJAX function?

    In that case:

    $("#mylink").click(function() {
     $.ajax({ type: "POST", url: "another.php", data: {id: "somedata"}, function(data) {
      $("#response").html(data);
     });
     return false;
    });
    
  • You could just use MooTools and http://mootools.net/docs/Request/Request.HTML

  • Krof is correct,

    One possible unwanted behavior of this however is that it will query the data every time the link is clicked. You can set the event to only call the ajax query once by using one.

    $("#mylink").one('click', function() {
      // ajax call
      return false;
    });
    

    Don't forget to set href="javascript:{}" in your link so after the event is fired once the link wont do anything;

    Krof Drakula : Actually the `href=""` part isn't needed if you cancel event bubbling by returning `false`. Although I think it does help if you want to avoid having the outline drawn around `a` elements (if I remember correctly).
  • I am calling one php file for graph. The returned content contains one alert mess. but It isnt executing. How to load that html string from the responce string

    strager : Is this another question? If so, create a new question ("Add Question" at top). Or is this in addition to the question? If so, edit your original question with your original account.

0 comments:

Post a Comment