0 Favourites

Need backup on PHP =(((

  • Friends, who'll tell me , how to load data from php array into the project? I mean, i have a php script that generates a data array , and now how an array via AJAX i can load into the project? dictionary-> asJSON-> AJAX.LastData or Array.JSON-> AJAX.Last data? I always get zeros. The idea is to create a turn-based multiplayer. Now i'm focused on how to set up the room. and it's not working =( all room in php database returnigng via AJAX.LastData as one big text, no separates =( Please, help me, how to setup php and c2-events correct?

    lik to php code --->

  • UP

    Axiomaltd

    Please don't bump after only 2 hours. It is acceptable to bump your own thread after 24 hours if no answer has been forthcoming.

    Be aware that those users who generally help come from varying time zones, so please have a little patience.

  • If you check the How do I FAQ in the AJAX category you will see some example of script and AJAX communication you can take example on.

  • i'm already seen them. but the problem is - how separate data, that c2 recieves via ajax, how to correctly put it to the array, and how LIST can create seperate item for each data in array?

  • Here are some of my old functions you might find helpful.

    protected function outputAsC2DisctionaryJSON($data)
    	{
    		$data = array(
    			'c2dictionary' => true,
    			'data' => $data
    		);
    		$this->outputData($data);
    	}
    	
    	protected function outputAsC2ArrayJSON($data, $arrayDimensions)
    	{
    		$data = array(
    			'c2array' => true,
    			'size' => $arrayDimensions,
    			'data' => $data
    		);
    		$this->outputData($data);
    	}
    	
    	private function outputData($data)
    	{
    		header('Content-Type: application/json');
    		echo json_encode($data);
    		exit(1);
    	}
    	
    	protected function outputSimpleData($data)
    	{
    		echo($data);
    		exit(1);
    	}[/code:oqfmxh5s]
    
    C2 has specific JSON format which these functions covers. As "data" you just provide an array. 
    In C2 you simply load dictionary with the AJAX.LastData.
    
    Also remember that you need to set the PHP to accept crossdomain requests if you want to do it for mobile or so.
  • AJAX

    How do I pull variables out of a PHP page using AJAX ? - LINK

    More about using PHP - LINK

    Various questions and answers on using AJAX with server-side scripting to make an online game - LINK

    Arrays

    Getting values from an array - LINK

    String to array example - LINK

    Filling an array with a string and tokens - LINK

    Concrete commented example of use of arrays - LINK

    Sorting an array - LINK

    Simple explanations on what is, how and why use arrays - LINK

    Another thread about using arrays - LINK

    Flood fill in an array - LINK

    Video explaining how to fill an array - LINK

    Fill a list from an array - LINK

    List

    Fill a list from an array - LINK

    Change list items by selection in another list - LINK

    Arrays, AJAX, List

  • thanx man ) really helps!

  • but here one little thing===>>>

    code for posting to php:

    <?php

    //?????????? ? ?????????? ?????? ??? ??????? ? ??

    $mysql_host = "localhost";

    $mysql_database = "123123123";

    $mysql_user = "123123123";

    $mysql_password = "123123123";

    $link = mysql_connect($mysql_host, $mysql_user, $mysql_password) or die("???????? ?????...");

    mysql_select_db($mysql_database, $link) or die ("??...");//???????????? ? ????

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

    header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");

    header("Content-Type: text/html; charset=UTF-8");

    if (isset($_POST['login'])) { $login = $_POST['login']; if ($login == '') { unset($login);} }

    if (isset($_POST['pass'])) { $pass=$_POST['pass']; if ($pass =='') { unset($pass);} }

    if (isset($_POST['data'])) { $data=$_POST['data']; if ($data =='') { unset($data);} }

    if (empty($login) or empty($pass) or empty($data))

    {

    echo ("?? ????? ?? ??? ??????????, ????????? ??? ????!");

    exit();

    }

    $login = stripslashes($login);

    $login = htmlspecialchars($login);

    $pass = stripslashes($pass);

    $pass = htmlspecialchars($pass);

    $data = stripslashes($data);

    $data = htmlspecialchars($data);

    $login = trim($login);

    $pass = trim($pass);

    $data = trim($data);

    $q1 = mysql_query("SELECT login FROM `data` WHERE `login`='".$login."'");

    $loginb = mysql_fetch_array($q1);

    $loginbd=$loginb['login'];

    if($loginbd == $login){

    echo("????????, ????????? ???? ????? ??? ???????????????.");

    exit();

    }

    else

    $result2 = mysql_query ("INSERT INTO data (login,pass,data) VALUES('$login','$pass','$data')");

    if ($result2=='TRUE')

    {

    echo "?? ??????? ????????????????!";

    }

    else {

    echo "??????! ?? ?? ????????????????.";

    }

    ?>

    and the code to retrive data to c2dictionary:

    <?php

    //?????????? ? ?????????? ?????? ??? ??????? ? ??

    $mysql_host = "localhost";

    $mysql_database = "123123123";

    $mysql_user = "123123123";

    $mysql_password = "123123123";

    $link = mysql_connect($mysql_host, $mysql_user, $mysql_password) or die("???????? ?????...");

    mysql_select_db($mysql_database, $link) or die ("??...");//???????????? ? ????

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

    header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");

    header("Content-Type: text/html; charset=UTF-8");

    //????????? ??????? ???????????? ??????

    if (isset($_POST['loglog'])) { $login = $_POST['loglog']; if ($login == '') { unset($login);} }

    if (isset($_POST['passwordlog'])) { $pass=$_POST['passwordlog']; if ($pass =='') { unset($pass);} }

    if (empty($login) or empty($pass))

    {

    echo ("?? ????? ?? ??? ??????????, ????????? ??? ????!");

    exit();

    }

    $login = stripslashes($login);

    $login = htmlspecialchars($login);

    $pass = stripslashes($pass);

    $pass = htmlspecialchars($pass);

    $data = stripslashes($data);

    $data = htmlspecialchars($data);

    $login = trim($login);

    $pass = trim($pass);

    $data = trim($data);

    //????????? ??????? ?????????? ????????

    if(isset($login) && isset($pass)){

    //?????? ? ?? ?? ????????? ?????? ??????

    $q1 = mysql_query("SELECT login FROM `data` WHERE `login`='".$login."' AND `pass`='".$pass."'");

    $loginb = mysql_fetch_array($q1);

    $loginbd=$loginb['login'];

    $q2 = mysql_query("SELECT pass FROM `data` WHERE `login`='".$login."' AND `pass`='".$pass."'");

    $passwordb = mysql_fetch_array($q2);

    $passwordbd=$passwordb['pass'];

    if($passwordbd == $pass){

    $r1 = mysql_query("SELECT data FROM `data` WHERE `login`='".$login."' AND `pass`='".$pass."'");

    $row = mysql_fetch_array($r1); // ?? ? ??????

    $arr = array('c2dictionary' => 'true',

    'data' => array('vin' => $row['vin'], 'year' => $row['year'],'mod' => $row['mod'],'phone' => $row['phone'],'fio' => $row['fio'],'mark' => $row['mark'])); // ?????? ???????? ?????? ???????

    echo json_encode($arr);

    }

    //???? ?? ?????? ?????? ?? ??????? 1qqqq

    else{

    echo '? ???? ??? ?? ??????? ?????????';

    }

    }

    ?>

    and here is the question - where i'm mistayken? no data recieve at all =((((

  • Just what I've noticed at first sight 'c2dictionary' => 'true', it should be 'c2dictionary' => true. I'm not sure if it's a mistake, it might work if C2 checks with == but will fail if it checks with ===.

    Anyway First of all I suggest to keep database credentials in some separate file. Also using some ORM DB library will give you better protection from mysql injection and your code will look much more tidy if you use some separate models for business operations, retrieving data etc.

    You also did not set header to JSON application. If you just send JSON, you should do it.

    Now what is exactly not working? Have you checked if AJAX.LastData has any content or you've checked the dictionary content after AJAX load?

    Is there a problem with connecting to PHP?

    Retrieving any data?

    Filling dictionary?

    PHP gives any error?

    Try to provide as much info as possible whenever you ask for help.

  • Construct 3

    Buy Construct 3

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

    Buy Now Construct 3 users don't see these ads
  • exactly nothing work as should be. ajax post dictionary.asjson, in database dict.json looks like {quot;quot and some values and so on}. in my code i didn't understand where and what i must to place or write. no injection protection is correnctly needed. just, help with the code, to make it work.

  • here is what load.php returns in ajax.last data

    {"c2dictionary":true,"data":{"vin":"123123123","year":"2015","mod":"A-classe","phone":"123","fio":"olejjon123","mark":"mercedes","count":1}}

    here, is the code to retrive this data

    $r1 = mysql_query("SELECT data FROM `data` WHERE `login`='".$login."' AND `pass`='".$pass."'");

    $result = mysql_fetch_array($r1);

    $r1=$result['data'];

    echo $r1;

    and dictionary must load json.ajax.lastdata. but he didn't do it. all keys are 0 =(((

    here is the code, that sending to DB dictionary.asjsonstring from c2 project:

    $q1 = mysql_query("SELECT login FROM `data` WHERE `login`='".$login."'");

    $loginb = mysql_fetch_array($q1);

    $loginbd=$loginb['login'];

    if($loginbd == $login){

    echo("sorry,it's already here.");

    exit();

    }

    else

    $result2 = mysql_query ("INSERT INTO data (login,pass,data) VALUES('$login','$pass','$data')");

    //$data is the dictionary.jsonstring which is sent by ajax.

    if ($result2=='TRUE')

    {

    echo "success";

    }

    else {

    echo "error";

    }

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