SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2011
    Posts
    48
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)

    php and $ajax - how do you send multiple fields back ?

    I'm using and ajax call with multiple fields

    Code:
    temp1='v1'; temp2='12'; temp3='Mary'; temp4='2'; temp5='A-'; temp6='unknown';
    $.ajax({
        type: 'POST',
        url: 'process.php',
        data: {
    		version: temp1,
    		sessionnumber: temp2,
    		name: temp3,
    		numofhours: temp4,
    		testresult: temp5,
    		gradepointaverage: temp6     
        },
        success: function(_log) {
             $('#readout').append(_log);
        }
    });
    and in process.php
    Code:
    $received = '<div>Data received for ' . $name .' '.$testresult. '</div>';
    echo $received;
    I want to append to $('#readout') a whole new set of data calculated off what was just sent. Such as a gradepointaverage calculated off other scores in mysql for that $name plus a new issued sessionnumber.

    Would anyone know how to mod this code so I am sending back multiple fields?
    Is it possible to originate an ajax call from the php end at a later point in time?

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Send back a JSON object as your response.

    PHP Code:
    $data = array('version' => 'something''name' => 'somethingelse');
    header("Content-Type: application/javascript");
    echo 
    json_encode($data); 
    Then you'll have an object in your AJAX callback instead of a string:

    Code:
        success: function(data) {
             alert(data.name);
             alert(data.version);
        }

  3. #3
    ♪♪ ♪ ♪ ♪ ♪♪ ♪ ♪♪ Markdidj's Avatar
    Join Date
    Sep 2002
    Location
    Bournemouth, South UK
    Posts
    1,551
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    What is the benefit of json callback over a javascript one?
    I would not use json and just return javascript which the browser parses rather than having to turn the json back in to javascript.
    LiveScript: Putting the "Live" Back into JavaScript
    if live output_as_javascript else output_as_html end if

  4. #4
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Not sure what you're talking about. JSON is a JavaScript response. As you can see, the AJAX callback gets a native JavaScript object you can directly access the properties of. You don't have to do anything to "turn the json back in to javascript".

  5. #5
    SitePoint Enthusiast
    Join Date
    Feb 2011
    Posts
    48
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    I will start testing with a json object – thank you for pointing me in this direction.

    Is it possible to initiate the ajax call from the server side?

  6. #6
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,870
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by Dan Grossman View Post
    You don't have to do anything to "turn the json back in to javascript".
    Strictly speaking what is passed back is a string representation of an object - to convert it into an actual object you would call JSON.parse() on the string (which reverses what the json_encode call did in the PHP).
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  7. #7
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by PrimeLens View Post
    Is it possible to initiate the ajax call from the server side?
    Can you put that another way?

    It doesn't make sense to say "initiate the ajax call from the server side", since an AJAX call is *by definition* initiated from JavaScript, which runs on the client.

  8. #8
    SitePoint Enthusiast
    Join Date
    Feb 2011
    Posts
    48
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by Dan Grossman View Post
    Can you put that another way?
    A situation where the server sends simple data or complex data (like json object) intermittently, the client side does not control the timing.
    What technology would this be ?

  9. #9
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by PrimeLens View Post
    A situation where the server sends simple data or complex data (like json object) intermittently, the client side does not control the timing.
    What technology would this be ?
    You do that through "long polling" or "Comet". The connection is still initiated by the client (via AJAX or websockets), but it sits open until the server responds -- intermittently, not immediately, only when there's something to send. When the client receives a response, it immediately starts a new connection to wait for the next message.

    If you intend to have a lot of users online, you'll need a different web server for this. The standard Apache or IIS setup won't be able to accomodate holding all those open connections very well.

  10. #10
    SitePoint Enthusiast
    Join Date
    Feb 2011
    Posts
    48
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    Got it! I was asking for my own curiosity and learning more than anything. My application will be just an ajax call with
    a json object passed back as you suggest.
    Thanks Dan

  11. #11
    ♪♪ ♪ ♪ ♪ ♪♪ ♪ ♪♪ Markdidj's Avatar
    Join Date
    Sep 2002
    Location
    Bournemouth, South UK
    Posts
    1,551
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    the server can respond with pure javascript, and a setTimeout or setInterval in pure javascript. So yes, the server can initiate intermittent responses.

    I would say this is the best way. You do not leave a connection open, and if your site starts to bottleneck you won't be adding to the problem by creating more client-side calls
    LiveScript: Putting the "Live" Back into JavaScript
    if live output_as_javascript else output_as_html end if


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •