14
02
Simple ajax driven php webistes

I have been getting many request from my junior followers to develop a simple ajax php websites. So this time i have showing you a simple website that build in php ajax  and bootstrap. The code is positioned in the body part. With the help of jquery ajax communication the code from from front end is send back to php (back-end). Also note that the addresses of the navigation links -#page . This part is called as a hash, which included in the URL. This will helpful when creating an entry in the browsers history it will display the appropiate content witout page refresh.

simple-ajax-php-website-hackandphp

First we have to create the html page.

HTML

  1.  <ul class="nav navbar-nav" id="navigation">  
  2.     <li id="home"> <a href="#home"> <span class="glyphicon glyphicon-home"></span> Home </a> </li>  
  3.     <li id="gallery"> <a href="#gallery"> <span class="glyphicon glyphicon-camera"></span> Gallery </a> </li>  
  4.     <li id="contact"> <a href="#contact"> <span class="glyphicon glyphicon-phone"></span> Contact Us </a> </li>  
  5.       </ul>  
  6. <div id="pageContent">  
  7. <div class="col-md-8">  
  8.             <div class="well well-sm">  
  9.                 <form>  
  10.                 <div class="row">  
  11.                     <div class="col-md-6">  
  12.                        <h1>What is Lorem-Ipsum?</h1>  
  13.                        <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. </p>  
  14.                     </div>  
  15.                     <div class="col-md-6">  
  16.                           <h1>What is Lorem-Ipsum?</h1>  
  17.                        <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. </p>  
  18.                     </div>  
  19.                     </div>  
  20.                      </form>  
  21.                 </div>  
  22.         </div>  
  23.         <div class="col-md-4">  
  24.         <div class="container-fluid">  
  25.         <img src="img/lorem_ipsum.jpg" class="img-responsive">  
  26.         </div>  
  27.         </div>  
  28.     </div>  

Jquery Ajax

  1. /** Show loading **/  
  2. function ajaxloading() {  
  3.     var obj = $(document.createElement('div')).attr("class""se-pre-con");  
  4.   
  5.     $("body").append(obj);  
  6.   
  7.     $(".se-pre-con").show();  
  8. }  
  9. /** hide loading **/  
  10. function closeajax() {  
  11.   
  12.     $(".se-pre-con").remove();  
  13. }  
  14. function loadPage(url) {  
  15.     url = url.replace('#''');  
  16.     $('ul li').removeClass('active'); //Remove the class active in li  
  17.     $('#' + url).addClass('active'); //Make the current li active  
  18.     ajaxloading();  
  19.     $.ajax({  
  20.         type: "POST",  
  21.         url: "load_page.php",  
  22.         data: 'page=' + url,  
  23.         dataType: "html",  
  24.         success: function(msg) {  
  25.   
  26.             if (parseInt(msg) != 0) {  
  27.                 $('#pageContent').html(msg);  
  28.                 closeajax();  
  29.             }  
  30.         }  
  31.   
  32.     });  
  33.   
  34. }  

Ajax is the technique used to send the request to the php and receive the response. The response is retrieving in the HTML content. Here the page name is send as the request, at first is basically check whether the $_POST['page'] is exists. If it is exists the content is send back as response to the jquery.

PHP

  1. <!--?php  
  2.   
  3. if(!$_POST['page']) die("0");  
  4.   
  5. $page = $_POST['page'];  
  6.   
  7. if(file_exists('pages/'.$page.'.html'))  
  8. echo file_get_contents('pages/'.$page.'.html');  
  9.   
  10. else {  
  11.       
  12. ?-->  
  13. <h2>404! Page Not Found</h2>  
  14.   
  15. <!--?php } ?-->  

That's all. Just check the live demo and download the source code and enjoy.

By posted on - 14th Feb 2016

Social Oauth Login

Personalized Map Navigation

Online Image Compression Tool

Image Compression

Advertisement

Recent Posts

Categories