AJAX [Returning Multiple Values]

Get help using Construct 2

Post » Wed Jun 10, 2015 10:05 pm

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;

Code: Select all
echo $var1
echo $var2
B
4
Posts: 19
Reputation: 240

Post » Thu Jun 11, 2015 4:47 am

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.
Sorry for my bad english, but I really do my best! And use and check with Google Translator.
B
11
S
4
G
1
Posts: 158
Reputation: 1,328

Post » Thu Jun 11, 2015 5:11 am

You can return a tokenized string or a JSON object that C2 can interpret.
B
74
S
31
G
26
Posts: 994
Reputation: 20,198

Post » Thu Jun 11, 2015 10:52 am

Magistross wrote: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
B
4
Posts: 19
Reputation: 240

Post » Thu Jun 11, 2015 12:24 pm

Make your PHP script to return a concatenated line like
Code: Select all
echo $var1 . "|" . $var2

( 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 system expressions "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
New to Construct ? Where to start

Image Image

Image Image

Please attach a capx to any help request or bug report !
Moderator
B
293
S
117
G
96
Posts: 7,310
Reputation: 71,069

Post » Thu Jun 11, 2015 4:49 pm

Multible values by json:

Multi row:
Code: Select all
$arr = array();
while($row = mysqli_fetch_array($result,MYSQLI_ASSOC))
{
        arr[] = $row;
}
echo json_encode($arr);

AJAX.LastData -> [{"name":"Bob","secondName":"Meyer","isMember":1},{"name":"Dieter","secondName":"Miller","isMember":1}]
Image

Single row(first result):
Code: Select all
$arr = array();
while($row = mysqli_fetch_array($result,MYSQLI_ASSOC))
{
        arr[] = $row;
}
echo json_encode($row[0]);

AJAX.LastData -> {"name":"Bob","secondName":"Meyer","isMember":1}
Image

Then use a json plugin to work with this strings. For example: Tools
Some examples:
Image

Sorry for spam, but my server is down and i am boring...
Last edited by deathangel1479 on Fri Jun 12, 2015 12:03 am, edited 7 times in total.
Sorry for my bad english, but I really do my best! And use and check with Google Translator.
B
11
S
4
G
1
Posts: 158
Reputation: 1,328

Post » Thu Jun 11, 2015 5:21 pm

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.
B
4
Posts: 19
Reputation: 240


Return to How do I....?

Who is online

Users browsing this forum: Yahoo [Bot] and 55 guests