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 we demonstrate how to request and parse JSON formatted PHP MySQL database data. To show the functionality we create a timed random MySQL JSON data request application using Ajax that you can run on your web pages.

JSON stands for JavaScript Object Notation. It is a lightweight format for creating and sending data objects between the server and your client-side applications. JSON is a part of JavaScript and not a separate entity, the notation syntax is built into the raw Javascript programming language as a subset.

source

32 thoughts on “json tutorial for beginners part 7 Ajax PHP MySQL Data Request Programming”

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

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

Your email address will not be published. Required fields are marked *