json tutorial for beginners part 7 Ajax PHP MySQL Data Request Programming




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”

  1. 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.

  2. 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>

  3. 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

  4. 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!

  5. 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

  6. 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.

  7. 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.

Leave a Reply