AJAX [Returning Multiple Values]

  • I've learned how to use AJAX, PHP and MySql to return database values but how do you handle multiple values?

    Does AJAX.lastvalue return an array or is there something I have to do to receive multiple values?

    In PHP do I have to do something special or simply;

    echo $var1
    echo $var2[/code:2vyw9z40]
  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • I cant say you what happens, but I can say you how you can try:

    --AJAX On "yourTag" completed

    -->Browser Log in console AJAX.LastData

    You have to experiment a bit.

  • You can return a tokenized string or a JSON object that C2 can interpret.

  • You can return a tokenized string or a JSON object that C2 can interpret.

    Could you possibly give me a small example and I'll work the rest out from there, thanks Magi

  • Make your PHP script to return a concatenated line like

    echo $var1 . "|" . $var2[/code:2yudkrsv]
    ( the dot "." should be the way to concatenate in PHP IIRC, to make sure, be sure to look into the PHP manual or a website about PHP programming).
    
    When you receive your AJAX line you can use the [url=https://www.scirra.com/manual/126/system-expressions]system expressions[/url] "tokenat", "tokencount", etc... to manipulate the string, according to the separator (in my example the "|" character).
    
    tokenat(AJAX.lastdata,0,"|") should allow you to only retrieve $var1; tokenat(AJAX.lastdata,1,"|") => $var2
  • Multible values by json:

    Multi row:

    $arr = array();
    while($row = mysqli_fetch_array($result,MYSQLI_ASSOC))
    {
            arr[] = $row;
    }
    echo json_encode($arr);
    [/code:hfj250k1]
    AJAX.LastData -> [{"name":"Bob","secondName":"Meyer","isMember":1},{"name":"Dieter","secondName":"Miller","isMember":1}]
    [img="https://dl.dropboxusercontent.com/u/95601836/multiphp2json.png"]
    
    Single row(first result):
    [code:hfj250k1]
    $arr = array();
    while($row = mysqli_fetch_array($result,MYSQLI_ASSOC))
    {
            arr[] = $row;
    }
    echo json_encode($row[0]);
    [/code:hfj250k1]
    AJAX.LastData -> {"name":"Bob","secondName":"Meyer","isMember":1}
    [img="https://dl.dropboxusercontent.com/u/95601836/singlephp2json.png"]
    
    Then use a json plugin to work with this strings. For example: [url=https://www.scirra.com/forum/plugin-tools-object-array-in-construct-based-on-json_t147493]Tools[/url]
    Some examples:
    [img="https://dl.dropboxusercontent.com/u/95601836/getLastData.png"]
    
    Sorry for spam, but my server is down and i am boring...
  • Thank you for all the help, I got my code working and managed to edit it to what I wanted.

    Great help again, thank you.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)