Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-14-2012, 11:20 AM   PM User | #1
alesko007
New to the CF scene

 
Join Date: Dec 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
alesko007 is an unknown quantity at this point
jQuery, AJAX and PHP fetching from database

Hello,

I'm working on a mobile app (I'm a total newbie) and I need some help. I'm trying to fetch data from MySQL database and encode it in JSON and then get it with AJAX call in jQuery.

HTML:
Code:
<ul></ul>
jQuery:
Code:
$(document).ready( function() {
 done();
});
 
function done() {
	  setTimeout( function() { 
	  updates(); 
	  done();
	  }, 200);
}
 
function updates() {
$.ajax({
     url:"http://wawhost.com/appProject/fetch.php",
     dataType: 'jsonp',
     success:function(data){
       $("ul").empty();
	   $.each(data.result, function(){
	    $("ul").append("<li>Name: "+this['name']+
		"</li><li>Age: "+this['age']+
		"</li><li>Company: "+this['company']+
		"</li><br />");
	   });
	   };
 });
}
PHP:
Code:
        include_once('db.php');
 
		$sql = "SELECT * FROM posts";
		$res = mysql_query($sql);
		$result = array();
 
		while( $row = mysql_fetch_array($res) )
		    array_push($result, array('name' => $row[0],
			                          'age'  => $row[1],
						  'company' => $row[2]));
 
		echo json_encode(array("result" => $result));
alesko007 is offline   Reply With Quote
Old 12-14-2012, 02:25 PM   PM User | #2
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
First of all, you should not call done() from inside the setTimeout callback. You should only call it after the request started by updates() finished, i.e. in its success callback

Second: There must not be a semicolon after the closing bracket of the success callback
Code:
$(document).ready(updates);
 
function updates() {
 $.ajax({
     url:"http://wawhost.com/appProject/fetch.php",
     dataType: 'jsonp',
     success:function(data){
       $("ul").empty();
       $.each(data.result, function(){
	    $("ul").append("<li>Name: "+this['name']+
		"</li><li>Age: "+this['age']+
		"</li><li>Company: "+this['company']+
		"</li><br />");
       });
       setTimeout(updates, 200);
     }
 });
}
This might not be the only issue here, but it should give you a good start :-)
devnull69 is offline   Reply With Quote
Reply

Bookmarks

Tags
ajax, jquery, json, php

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:11 AM.


Advertisement
Log in to turn off these ads.