Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML - Change\Update page contents without refreshing\reloading the page

I get the data from DB and display it in a div... what I want to do is when I click a link it should change the content of the div

one option is to pass parameter through URL to itself and reload the page...

I need to do it without reloading\refreshing...

<?php        $id   = '1';      function recp( $rId ) {         $id   = $rId;     } ?>  <a href="#" onClick="<?php recp('1') ?>" > One   </a> <a href="#" onClick="<?php recp('2') ?>" > Two   </a> <a href="#" onClick="<?php recp('3') ?>" > Three </a>  <div id='myStyle'> <?php     require ('myConnect.php');          $results = mysql_query("SELECT para FROM content WHERE  para_ID='$id'");      if( mysql_num_rows($results) > 0 ) {         $row = mysql_fetch_array( $results );         echo $row['para'];     } ?> </div> 

The goal is when I click any of the links the contents of the div and php variable\s gets updated without refreshing.... so that user could see new data and after that if some query is performed it is on new variable\s

p.s I know it is gonna require some AJAX but I don't know AJAX.. so please reply with something by which I can learn... my knowledge is limited to HTML, PHP, some JavaScript & some jQuery

like image 506
Moon Avatar asked Sep 05 '10 01:09

Moon


People also ask

How do I change the content of a page without reloading it?

AJAX allows web pages to be updated asynchronously by exchanging small amounts of data with the server behind the scenes. This means that it is possible to update parts of a web page, without reloading the whole page. Classic web pages, (which do not use AJAX) must reload the entire page if the content should change.

How do I reload a DIV without reloading the whole page?

Use this. $('#mydiv'). load(document. URL + ' #mydiv');

How do I restrict page refresh?

Click the Start button, type “internet options” and select Internet Options in the search results. In the Internet Properties window, click “Custom tab -> Custom level,” then in the Security Settings window, scroll down until you find “Allow META REFRESH.” Disable this option and click OK.


2 Answers

jQuery will do the job. You can use either jQuery.ajax function, which is general one for performing ajax calls, or its wrappers: jQuery.get, jQuery.post for getting/posting data. Its very easy to use, for example, check out this tutorial, which shows how to use jQuery with PHP.

like image 34
Sorantis Avatar answered Sep 24 '22 06:09

Sorantis


You've got the right idea, so here's how to go ahead: the onclick handlers run on the client side, in the browser, so you cannot call a PHP function directly. Instead, you need to add a JavaScript function that (as you mentioned) uses AJAX to call a PHP script and retrieve the data. Using jQuery, you can do something like this:

<script type="text/javascript"> function recp(id) {   $('#myStyle').load('data.php?id=' + id); } </script>  <a href="#" onClick="recp('1')" > One   </a> <a href="#" onClick="recp('2')" > Two   </a> <a href="#" onClick="recp('3')" > Three </a>  <div id='myStyle'> </div> 

Then you put your PHP code into a separate file: (I've called it data.php in the above example)

<?php   require ('myConnect.php');        $id = $_GET['id'];   $results = mysql_query("SELECT para FROM content WHERE  para_ID='$id'");      if( mysql_num_rows($results) > 0 )   {    $row = mysql_fetch_array( $results );    echo $row['para'];   } ?> 
like image 113
casablanca Avatar answered Sep 21 '22 06:09

casablanca