Ajax sample code with Json/PHP

Discussion in 'Javascript' started by Otto Wyss, Apr 9, 2007.

  1. Otto Wyss

    Otto Wyss Guest

    I've now been looking for a week for a simple but useful sample on how
    to get a list of entries (persons) via an XMLHttpRequest using Json/PHP
    on the server. So far I've found about a thousend different tutorials
    and code samples but not a single one, where the server returns an array
    of entries. Very few samples use Json at all and almost none show the
    server code. So does anybody know a sample which

    - uses just a small javascript library (preferable jQuery)
    - uses Json/PHP and returns a list (array) of entries
    - contains the full code, server and client side
    - hopefully has a working demo on the web

    Or does somebody have such a sample to share or put up on the web?

    O. Wyss
     
    Otto Wyss, Apr 9, 2007
    #1
    1. Advertising

  2. Otto Wyss

    Marc Guest

    "Otto Wyss" <> wrote in message
    news:461a486d$...
    > I've now been looking for a week for a simple but useful sample on how to
    > get a list of entries (persons) via an XMLHttpRequest using Json/PHP on
    > the server. So far I've found about a thousend different tutorials and
    > code samples but not a single one, where the server returns an array of
    > entries. Very few samples use Json at all and almost none show the server
    > code. So does anybody know a sample which
    >
    > - uses just a small javascript library (preferable jQuery)
    > - uses Json/PHP and returns a list (array) of entries
    > - contains the full code, server and client side
    > - hopefully has a working demo on the web
    >
    > Or does somebody have such a sample to share or put up on the web?
    >
    > O. Wyss


    I have the following sample running only on my development computer... it's
    a very basic sample, no error handling or other gadgets...

    Get "AjaxRequest.js" from: http://www.ajaxtoolbox.com/request/source.php
    (okay, this is the only gadget but a very basic and small(!!) library,
    also.. read what Matt Kruse is writing!)

    put the following 3 files in the same directory and run index.php

    json.txt

    {"employees":[
    {"name": "John", "id": 10},
    {"name": "Jane", "id": 12},
    {"name": "Tom", "id": 13},
    {"name": "Jones", "id": 15}
    ]
    }


    index.php

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <title>Sample 1</title>
    <script type="text/javascript" src="AjaxRequest.js"></script>
    <script type="text/javascript">
    //<![CDATA[
    var jsonobject;
    window.onload = function(){
    AjaxRequest.post(
    {'url':'get.php', 'parameters':{'list':'employees'}
    ,'onSuccess':function(req){
    jsonobject = eval('('+req.responseText+')');

    /* get the select element and fill the list from the json string */
    var sel = document.getElementById("list1");
    for (var i=0; i < jsonobject.employees.length;i++){
    addOptions(sel, jsonobject.employees.id,
    jsonobject.employees.name);
    }
    }, 'onError':function(req){ alert("Error!\nStatusText="+req.statusText);}
    });
    }
    function addOptions(object, oValue, oText) {
    /* very simple (old skool) method of adding list options */
    var defaultSelected = true; var selected = true;
    var optionName = new Option(oText, oValue, defaultSelected, selected)
    var length = object.length;
    object.options[length] = optionName;
    }
    //]]>
    </script>
    </head>
    <body>

    <select name="list1" id="list1" size="4">
    </select>

    </body>
    </html>


    get.php

    <?php

    $what = $_GET["list"];
    if ($what = "employees"){
    $file = "json.txt";
    header("Content-type: application/json; charset=utf-8");
    header("Content-Transfer-Encoding: 8bit");
    header("Content-Length: " . filesize($file));
    print(file_get_contents($file));
    }else{
    // another list?
    }
    ?>
     
    Marc, Apr 9, 2007
    #2
    1. Advertising

  3. Otto Wyss

    wyo Guest

    On Apr 9, 8:24 pm, "Marc" <> wrote:
    >
    > Get "AjaxRequest.js" from:http://www.ajaxtoolbox.com/request/source.php
    > (okay, this is the only gadget but a very basic and small(!!) library,
    > also.. read what Matt Kruse is writing!)
    >

    I must say I've already looked into Ajaxtoolbox but I've some
    reservation to base a new development onto a source which hasn't
    changed since June 22, 2005. Also the serializeForm doesn't use JSON
    which IMO is a must. Anyway thanks a lot for the sample.

    > get.php
    > <?php
    > $what = $_GET["list"];
    > if ($what = "employees"){
    > $file = "json.txt";
    > header("Content-type: application/json; charset=utf-8");
    > header("Content-Transfer-Encoding: 8bit");
    > header("Content-Length: " . filesize($file));
    > print(file_get_contents($file));
    > }else{
    > // another list?
    > }
    > ?>


    Why do you use headers in get.php? Are they necessary?

    O. Wyss
     
    wyo, Apr 10, 2007
    #3
  4. Otto Wyss

    Marc Guest

    <snip>
    >
    >> get.php
    >> <?php
    >> $what = $_GET["list"];
    >> if ($what = "employees"){
    >> $file = "json.txt";
    >> header("Content-type: application/json; charset=utf-8");
    >> header("Content-Transfer-Encoding: 8bit");
    >> header("Content-Length: " . filesize($file));
    >> print(file_get_contents($file));
    >> }else{
    >> // another list?
    >> }
    >> ?>

    >
    > Why do you use headers in get.php? Are they necessary?
    >
    > O. Wyss
    >


    I use the headers because the RFC says so:
    http://www.ietf.org/rfc/rfc4627.txt?number=4627


    > header("Content-type: application/json; charset=utf-8");


    3. Encoding
    JSON text SHALL be encoded in Unicode. The default encoding is UTF-8.

    6. IANA Considerations
    The MIME media type for JSON text is application/json.

    > header("Content-Transfer-Encoding: 8bit");


    6. IANA Considerations
    Encoding considerations: 8bit if UTF-8

    > header("Content-Length: " . filesize($file));

    because you simply should tell the client what size you are sending...

    Most browsers don't care if you send the headers or not but in the future
    other clients (read software) might want to use your json also...
     
    Marc, Apr 10, 2007
    #4
  5. Otto Wyss

    Marc Guest

    > <snip>
    >>
    >>> get.php
    >>> <?php
    >>> $what = $_GET["list"];
    >>> if ($what = "employees"){
    >>> $file = "json.txt";
    >>> header("Content-type: application/json; charset=utf-8");
    >>> header("Content-Transfer-Encoding: 8bit");
    >>> header("Content-Length: " . filesize($file));
    >>> print(file_get_contents($file));
    >>> }else{
    >>> // another list?
    >>> }
    >>> ?>

    >>
    >> Why do you use headers in get.php? Are they necessary?
    >>
    >> O. Wyss
    >>

    >
    > I use the headers because the RFC says so:
    > http://www.ietf.org/rfc/rfc4627.txt?number=4627
    >
    >
    >> header("Content-type: application/json; charset=utf-8");

    >
    > 3. Encoding
    > JSON text SHALL be encoded in Unicode. The default encoding is UTF-8.
    >
    > 6. IANA Considerations
    > The MIME media type for JSON text is application/json.
    >
    >> header("Content-Transfer-Encoding: 8bit");

    >
    > 6. IANA Considerations
    > Encoding considerations: 8bit if UTF-8
    >



    And oh... the code sample just sends the json string as whatever your
    filesystem is using as encoding...
    to send real utf-8 you could use the 'utf8_encode' function in php...
     
    Marc, Apr 10, 2007
    #5
  6. Otto Wyss

    Otto Wyss Guest

    Marc wrote:
    >>>> ?>
    >>> Why do you use headers in get.php? Are they necessary?
    >>>

    >
    > And oh... the code sample just sends the json string as whatever your
    > filesystem is using as encoding...
    > to send real utf-8 you could use the 'utf8_encode' function in php...
    >

    Thanks a lot, I just was curious since no other samples uses headers or
    utf8_encode. I'll keep that in mind.

    O. Wyss
     
    Otto Wyss, Apr 10, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rajive Narain
    Replies:
    0
    Views:
    2,016
    Rajive Narain
    Sep 18, 2009
  2. Florian Frank
    Replies:
    0
    Views:
    259
    Florian Frank
    Jun 30, 2009
  3. Pugi!
    Replies:
    6
    Views:
    306
    David Mark
    Jan 12, 2008
  4. sajuptpm
    Replies:
    2
    Views:
    376
    sajuptpm
    Dec 28, 2012
  5. Acácio Centeno
    Replies:
    1
    Views:
    302
    dieter
    Feb 15, 2013
Loading...

Share This Page