Home › Forums › Programming › re: Fetch/Extract Data From Database Without Refreshing Webpage: jQuery
Tagged: jquery json php
- This topic has 2 replies, 2 voices, and was last updated 12 years ago by rw1.
- AuthorPosts
- November 4, 2012 at 7:18 am#2497rw1Member
hello,
i’ve decided to move queries regarding the excellent post ‘Fetch/Extract Data From Database Without Refreshing Webpage: jQuery‘ here as it might be easier to manage.
first issue – resolved
i have used the code as is in the original post and was getting the error message ‘SyntaxError: unterminated string literal’.
when i ran the my_script.js code through jslint.com it also showed the error message ‘Unclosed string’.
i have found out one solution to this problem was to escape the end of the line with a backslash character, so the code becomes:
output.html:
< !DOCTYPE html>
Fetch/Extract Data From Database: jQuery + JSON + PHP+ AJAX db.php
< ?php $conn = mysql_connect('localhost', 'user_name', 'password'); $db = mysql_select_db('user_dbname'); ?>
fetch.php
< ?php include_once('db.php'); $sql = "SELECT * FROM data LIMIT 1, 10"; $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)); ?>
my_script.js
$(document).ready( function() { done(); }); function done() { setTimeout( function() { updates(); done(); }, 200); } function updates() { $.getJSON("fetch.php", function(data) { $('ul').empty(); $.each(data.result, function(){ $("ul").append("
- Name: " + this['name'] + "
\- Age: " + this['age'] + "
\- Company: "+this['company']+"
\ "); }); }); }i also removed the
as apparently these are not meant to be within- ‘s?
second issue – not resolved yet
i have 100 entries in the database and none of this information is being output on the page ie it is a blank page.
‘firebug > console > all’ shows incremental lines of: “GET: http :// path/to//fetch.php 200 OK 283ms” but nothing shows on the page.
thank you.
November 7, 2012 at 2:49 pm#2520SatishKeymasterI went through the code and ran it on my computer successfully :-)
Remove those additional slashes in my_script.js
Mistake in linking jQuery library file in output.html
Have the jquery-1.8.2.min.js in the same folder and link to it as below:November 11, 2012 at 9:34 am#2531rw1Memberthank you very much for testing this.
i have tested the same code and removed all slashes and put jquery within the same directory and there is no data being output.
firebug shows:
“Firebug’s log limit has been reached. 890 entries not shown” and the number keeps increasing.
and as before:
‘firebug > console > all’ shows incremental lines of: “GET: http :// path/to//fetch.php 200 OK 283ms” but nothing shows on the page.
thank you.
- AuthorPosts
- You must be logged in to reply to this topic.