javascript - PHP SQL / Ajax retrievement issues -
i have web application data mysql database , put in website of handlebars templating.
first of all, php use looks this:
<?php function isxhr() { return isset( $_server['http_x_requested_with'] ); } function connect(){ global $pdo; $pdo = new pdo("mysql:host=localhost;dbname=personal", "user", "password"); } function get_blog_posts( ) { global $pdo; $stmt = $pdo->prepare(' select url, title, location, date, author, content, thmb photolog limit 50'); $stmt->execute(); return $stmt->fetchall( pdo::fetch_obj ); }
then use javascript function call inside document.ready(...)
looks like:
function loadblogposts(){ var photos = { init: function( config ) { this.config = config; this.setuptemplates(); this.fetchphotos(); this.helpers(); $.ajaxsetup({ url: 'index.php', type: 'post' }); }, setuptemplates: function() { this.config.photolisttemplate = handlebars.compile( this.config.photolisttemplate); }, helpers: function(){ handlebars.registerhelper("foreach",function(arr,options) { if(options.inverse && !arr.length) return options.inverse(this); return arr.map(function(item,index) { item.$index = index; item.$first = index === 0; item.$last = index === arr.length-1; item.$rest = index >= 1; return options.fn(item); }).join(''); }); }, fetchphotos: function() { var self = photos; $.ajax({ datatype: 'json', success: function(results) { self.config.photoslist.empty(); for(i=0;i<results.length;i++){ results[i].author = results[i].author.tolowercase(); results[i].title = results[i].title.touppercase(); results[i].location = results[i].location.touppercase(); } if ( results[0] ) { self.config.photoslist.append( self.config.photolisttemplate( results.reverse() ) ); } else { self.config.photoslist.append('<li>nothing returned.</li>'); } } }); } }; photos.init({ photolisttemplate: $('#photo_blog_template').html(), photoslist: $('div#blog_list') }); }
and put data template:
<div id="blog_list"> <script id="photo_blog_template" type="text/x-handlebars-template"> {{#foreach this}} {{#if $first}} <div class="4u"> <div class="photo"> <a id="single_image" href="{{url}}"><img src="{{thmb}}" alt=""/></a> </div> </div> <div class="8u post"> <header> <h2>{{title}}</h2> <h3>{{location}}</h3> <div class="antet"> <div class="author">{{author}}</div> <div class="date">{{date}}</div> </div> </header> <p>{{content}}</p> </div> {{/if}} {{#if $rest}} <div class="3u post-small"> <div class="photo"> <a id="single_image" href="{{url}}"><img src="{{thmb}}" alt=""/></a> </div> <header> <h2>{{title}}</h2> <h3>{{location}}</h3> <div class="antet"> <div class="author-small">{{author}}</div> <div class="date-small">{{date}}</div> </div> </header> <p>{{content}}</p> </div> {{/if}} {{/foreach}} </script> </div>
the problem appears on new android browsers, , after close application/page , open ctrl+shift+t. doesn't show page, shows json file. page @ stefanperju.com
Comments
Post a Comment