variables php on construct2???

Discussion and feedback on Construct 2

Post » Sun Jan 12, 2014 6:47 pm

Hi everybody,

I made a code in php for save some variable in php, the file is this:

<?php
$variable1= 100;
$variable2 = $variable1;
?>

I know this forum is not for php, but the problem is when I try to read the variable in construct2 with ajax because i don't understand the logic of construct 2.

i need to set one variable in construct2 with a $variable2 of php file.

the file:

.capx

.php file

thank you so much, I really need to understand that, actually i spent 4 hours for that
B
21
S
9
Posts: 298
Reputation: 2,967

Post » Sun Jan 12, 2014 7:11 pm

Hey man, i'm wainting for a new version of C2 to download, so i cannot open you capx, but i think this should get you going in the right direction:

Your php could look something like this:

<?php
header('Access-Control-Allow-Origin: *');

$variable1= 100;
$variable2 = $variable1;


echo $variable2;

?>

the header is here to enable CORS, so you can test this from localhost, echo is actually returning the variable value when the ajax call is made.

So in construct you can do:
----
global variable "ConstructVar" = 0;

on trigger -> AJAX request URL "http://mydomain.com/variable.php" (tag "php")


On "php" completed -> set ConstructVar to Ajax.LastData
---







Be nice until it's time to not be nice
B
36
S
10
G
9
Posts: 293
Reputation: 6,712

Post » Sun Jan 12, 2014 7:12 pm

Hi,

so first of all you wanna change your AJAX Request to "http://yourserver.de/variable.php?variable2=" & variable2inConstruct.

There is no need for the $ sign here and you need to specify your server adress.

Second you need to output the content of your processed variable in php. I.g. " echo $variable2; "

Should work.

Btw. to post the content of your php file you need to serve it as txt. People wont be able to download your php file when its served as php.
B
23
S
7
G
4
Posts: 154
Reputation: 3,749

Post » Sun Jan 12, 2014 7:20 pm

In the How do I FAQ, section AJAX :
How do I pull variables out of a PHP page using AJAX ? - LINK

In the tutorials:
AJAX example with Construct2; Online High Score Table (AJAX, PHP, MySQL).
New to Construct ? Where to start

Image Image

Image Image

Please attach a capx to any help request or bug report !
Moderator
B
296
S
119
G
96
Posts: 7,332
Reputation: 71,306

Post » Sun Jan 12, 2014 9:09 pm

[QUOTE=Kyatric] In the How do I FAQ, section AJAX :
How do I pull variables out of a PHP page using AJAX ? - LINK

In the tutorials:
AJAX example with Construct2; Online High Score Table (AJAX, PHP, MySQL).
[/QUOTE]

thask for the reply, I follow this example but doesn't work, I didn't understand the logic...

now is working, but if I use the:

<?php

header('Access-Control-Allow-Origin: *');
require 'gold.php';

echo $gold;
?>

the variable in construct 2 will be "NaN"

gold.php is this:

<?php
header('Access-Control-Allow-Origin: *');
require 'facebook_login.php';

///////////////////
                      if ( $loadspecific == "caricadati" ) {
        $query = "SELECT goldDB FROM tabletest WHERE UIDfacebook = $id";
        $result2 = mysql_query(mysql_real_escape_string($query)) or die (mysql_error());

        while($row = mysql_fetch_array($result2))
        {
              //echo "\\";
            
              $gold = "{$row['goldDB']}" ;
              //echo $gold;
              
              //echo $_GET['gold'];
        }

        }else{
            //echo $result;

            }

?>

I start to make a project with facebook... now my project save the data from facebook API in my database and load the single information in the database (gold.php)

now my problem is load the variable in Construct2 because in php is working

i'm thinking if the problem is "while($row = mysql_fetch_array" but the echo will be a single number like "1111", but Construct2 doesn't load the variable if I insert require 'facebook_login.php';

the variable is from my database...Ribis2014-01-12 21:11:22
B
21
S
9
Posts: 298
Reputation: 2,967


Return to Construct 2 General

Who is online

Users browsing this forum: No registered users and 9 guests