Lesson Code: http://www.developphp.com/video/JavaScript/JSON-Timed-Ajax-PHP-MySQL-Data-Request-Web-Application Part 7 of learning JSON for beginners with Adam. In this final lesson of the series…
Original source
Lesson Code: http://www.developphp.com/video/JavaScript/JSON-Timed-Ajax-PHP-MySQL-Data-Request-Web-Application Part 7 of learning JSON for beginners with Adam. In this final lesson of the series…
Original source
33 responses to “json tutorial for beginners part 7 Ajax PHP MySQL Data Request Programming”
HI, which "json" file did you use for this particular tutorial? Sorry, I must have missed it. I did follow all your other JSON tutorials previous and post, but never saw the JSON file used for this particular tutorial. Trying to use my gallery JSON file and nothing is working. I've had much success till now with all your other tutorials I have followed. Can you help me with this one. Thanks.
whats with the bot voice smdh
Please note that the SQL code in here is depreciated and will not work with new php. You can find equivalent code by googling php sql and finding the code there.
plz tell whats wrong in this im not getting the output…its just show redirecting
json_mysql_data.php
<?php
header("Content-Type: application/json");
if(isset($_POST['limit'])){
$limit = preg_replace('#[^0-9]#', '', $_POST['limit']);
require_once("db_conx.php");
$i = 0;
$jsonData = '{';
$sqlString = "SELECT * FROM users ORDER BY RAND() LIMIT $limit";
$query = mysqli_query($db_conx,$sqlString) or die (mysql_error());
while ($row = mysql_fetch_array($query)) {
$i++;
$id = $row["id"];
$name = $row["name"];
$pass = $row["pass"];
$jsonData .= '"person'.$i.'":{"id":"'.$id.'","name":"'.$name.'", "pass":"'.$pass.'" },';
}
$jsonData = chop($jsonData, ",");
//$now = getdate();
//$timestamp = $now[0];
//$jsonData .= '"arbitrary":{"itemcount":'.$i.', "returntime":"'.$timestamp.'"}';
$jsonData .= '}';
echo $jsonData;
}
?>
html file
<html>
<head>
<style>
div#databox {
padding:12px;
background: #F3F3F3;
border:#CCC 1px solid;
width:550px;
height:310px;
}
</style>
<script>
var myTimer;
function ajax_json_data(){
var databox = document.getElementById("databox");
var arbitrarybox = document.getElementById("arbitrarybox");
var hr = new XMLHttpRequest();
hr.open("POST", "json_mysql_data.php", true);
hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
hr.onreadystatechange = function() {
if(hr.readyState == 4 && hr.status == 200) {
var d = JSON.parse(hr.responseText);
arbitrarybox.innerHTML = d.arbitrary.returntime;
databox.innerHTML = "";
for(var o in d){
if(d[o].name){
databox.innerHTML += +d[o].id;
databox.innerHTML +=+d[o].name;
databox.innerHTML += +d[o].pass;
}
}
}
}
hr.send("limit=4");
databox.innerHTML = "requesting…";
//myTimer = setTimeout('ajax_json_data()',6000);
}
</script>
</head>
<body>
<h2>Timed JSON Data Request Random Items Script</h2>
<div id="databox"></div>
<div id="arbitrarybox"></div>
<script>ajax_json_data();</script>
</body>
</html>
Nice, but where is connect_db.php?
Pronounced "jason" like the name bro
great lessons..explained very well.
Very useful tutorial.. Thank you. This really helps me working on my thesis.
Great job Adam you are the Poo!
Excellent tutorial. Worked after the first time (well I missed one bracket due to a typing error). Very straight forward!
How tables for this example should look like (tables names ect)? help
Not working for me. Help! … php doesn't go pass … if (isset($_POST['limit']))
just what I needed, thanks dude
Hi Adam, found your tutorial series on my search for ressources for JSON to get started doing some stuff with android apps and getting data from mysql for this.
The videos are very well done, spoken slowly and good understandable (special when not native english speaking), giving the needed information to each used function at right time and an overview of the co-working of each function with the other.
They gave me a very good basic overview how to get started with JSON, thank you very much for the time you spend in this videos. Will keep an eye on your websites! Br Dom
Hmm, what preg_replace do then? ._. I kinda confuse at there.
You can call the data from mysql with php, then pass the array result into php's json_decode() function.
Do you have any tutorial for getting data from mysql database and and display using json ????
how to get this file doc (PDF)
I owe you a massive thank you! Your tutorials saved me. Got 100% on my assignment thanks to you 🙂
suppose there are no numbers in the limit variable?
Hi Adam,
I have a problem to view the page I have copied the source codes and put them into the live server to test, it shows nothing but the request… Not sure what else I need to do.I have just copied the part 3 tutorial source code from your youtube site. Any helps would be greatly appreciated!
Thanks Adam, Nice video series, Now I have good knowledge on JSON from basic. Thanks once again for your Amazing and excellent videos..
DON'T FORGET THE GODDAMN FANLOADER!!! good video series!
Hey Adam thanks for the great videos… do you have any tutorials that explain websockets?
How can we show an IMAGE from our DATABASE by the side of each. images can be jpg,JPEG,gif, and all other common image formats
dude i gotta thank you, i knew nothing about web development two weeks ago, now i'm coding in html, css, php, javascript, using jquery and ajax… now json. cant wait till the new web intersect videos come out. send me a msg back if u wanna check what i did with the site so far
Adam, can you insert an affiliate component in the new project
Salute to you for settling the issue with JSON, Thanks
please make some socket server programming tutorials (without html5's websockets)
Great job mang! This is really sweet.
haha, I saw your shortbus images on your desktop… too funny mang
GREAT video your a genius can you help me build a buy friend (user) as a pet game called pets (similar to tagged dot com pets game where each user has a value and can be owned by other users. i have a few files but got stuck at how the user is displayed i can add a user that plays pets into the pets database and then got stuck on the link that a user can click to purchase / own a user on the pets game.
Don't use Ajax requests for that. I would not request or check database at intervals less than 10 seconds using Ajax. Look into socket server programming for that. PHP and JavaScript both are capable of socket server engineering. HTML5 also has websockets. Node.js and socketio are Frameworks for socket server programming.