Problem with the AJAX on Complete and on Error triggers

0 favourites
  • 9 posts
From the Asset Store
Game Triggers is as versatile as a Swiss army knife, and will be a great resource for any type of game you're creating.
  • Hello people!

    I'm having problems with the AJAX On Complete and On Error triggers.

    I'm trying to compare a text that the user put in my game with one in my SQL Database, to see if it already exists and add this text to an Unique Variable.

    He enters it and then I Send to URL "http://xyz.blablabla/user/user.php?name="&userEntry.txt" (method "POST", tag "SaveName");

    With that done, it runs my php code:

    if(isset($_GET['name'])){
    
         $name = strip_tags(mysql_real_escape_string($_GET['name']));
         $sql = mysql_query("INSERT INTO `$db`.`names` (`id`,`name`) VALUES ('','$name');");
         
         if($sql){
         
              //The query returned true.
              echo 'This name has been added to the Database';
              
         }
         if(!$sql){
         //The query returned false.
              echo 'This name already exists in the Database. Put another';
         }
    }[/code:lgd9stpg]
    Until here, everything is ok. The code is running well and working.
    
    Getting back to Construct, I put the SaveName Trigger:
    On "SaveGame" Completed (The query returned true): Set True Variable to 1
    On "SaveGame" Error: (The query returned false): Set Set Error Variable to 2
    
    ___________________________
    Running this, and putting the text to compare to the SQL Database, everything goes fine. If the name is new it adds to the Database, and if not it don't add.
    
    BUT the game returns True 1 and Error 2 in any case I test. If the name exists or not, the game return 1 and 2. It can't happen because I wanna make that if returns True 1 it goes to the next layout and if returns False 2 it DON'T go and appears an text asking the player to put another name.
    
    Can someone help me out with this? The game is almost complete and I really want to release it (will be my first game released  ), but this problem is giving me headache. =(
    
    If you guys needs any Screenshot to help me out I post, but I don't think it's necessary.
    
    Thank you all in advance!
  • If I read your statement correctly then I think your using the On Error incorrectly? The On Error is triggered when ever the Ajax Request fails. (This almost never happens as long as the server is up). It should always complete and return what ever you echo back to the screen. You will need in the On Complete action to compare the Ajax.LastData (You should print this to screen in a text box for testing) to the 2 possible outcomes whether the name is valid or if it has already been used. As to why both the On Complete and On Error are triggering is bit of a mystery but that's moot considering your using the On Error incorrectly anyway.

  • I've put Ajax.LastData on a Text Object when On Complete triggers, but nothing appears there.

    Do you have another way of comparing a data that the user entry with one in the SQL Database?

  • Hey guys.

    I am willing to pay somebody 10 000 euro. if he or she or they, build a solid plugin for construct.

    Features:

    -Authentication layer

    -High score feature

    -..

    If we are going to do business, we are going to have a talk and discus all the details of it.

    Spread the word

    Have a good one

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • NicholasMDS Sounds like your not getting any ajax data back. If your running your capx in preview mode and not running from the same the server the AJAX file is located on, then your server will by default ignore the ajax request. Its a security function of php to prevent outside connections from accessing any data you don't want them to. On your AJAX file you need to add a PHP header call to allow access from out side connections.

    <?php 
    
    header('Access-Control-Allow-Origin: *');
    
    ?>[/code:wjqqpfrg]
    
    How ever you need to do more reading on this subject as this line above will open up your server to all outside requests. Its dangerous to leave it in place as is but for testing this should fix your problem
  • Hey troublesum, thank you very much for your support.

    But I have only one last question that probably will solve my problem...

    I managed to return the AJAX.LastData, but I'm returning "<\br>".

    I have a database with 3 Int Values in the table: the id(AutoIncrement), int nameIsOk and int nameIsNotOk, that I'm correctly filling with the values 1 (if Name is ok) and 2 (if Name isn't ok).

    I want to get the NameIsOk int value and print it in my GameScreen, but what I'm getting in the text box is a "<\br>". And if I try to print it from a Global Number Variable it gets "NaN" (wich I think means Not a Number).

    What I'm doing in the code is the following:

    // Retrieving data from database
    $sql="SELECT * FROM nameIsOk ORDER BY nameIsOk DESC LIMIT 05";
    $result=mysql_query($sql);[/code:8ava4v53]
    
    I know that probably is missing some coding and probably that the "$sql=" code is wrong, but I don't know the sintax of PHP, I only know some programming in Java and C++...
    
    If you can help me with this last thing, I'll be a lot grateful!
    
    Thank you again for your attention and support.
  • NicholasMDS Its strange that you are getting "<br/>". That means your PHP file that the Ajax request is accessing is returning HTML as well? I can't really help you on where that HTML coming from. Check your file or create a new one. If you willing to attach your PHP file i can take a look at it.

    Secondly that SQL looks does wrong. You have "SELECT * FROM nameIsOk" but you made it sound like "nameIsOk" is a column in the table. Is that also the table name as well? If not then there is one problem and if so I would recommend changing the table or column name so they dont match. Everything should still work but it will be less confusing.

    Do you self a favor and don't store the AJAX.Lastdata in a global number variable. If the AJAX.Lastdata ends up being string because it failed you will get the NaN (Not a Number) value. Just store it a as a string and work with it like that. As you PHP and C2 skills get better you should look into sending and recieving data as JSON (dont worry about that now but it will be useful to learn later).

  • Hey troublesum

    nameIsOk and nameIsNotOk are columns on my "names" table.

    I was getting <\br> for an error in my Password, but now it's ok.

    I managed to get numbers from the Code to Construct 2, finally. But one thing isn't right:

    $sql="SELECT * FROM nameIsOk ORDER BY id";
    $result=mysql_query($sql);
    
    echo intval($result);
    
    //echo 'hi';[/code:kofd3k1x]
    
    With this code, I get 0 from the $result. If I put echo intval(1), I get the number 1, so it's happening a problem when storing the variable of nameIsOk column to the $result Variable (the idea is if nameIsOk column == 1, the name is ok, if nameIsOk column == 0, the name isn't ok).
    
    Don't know if you can help me store correctly the nameIsOk value to the $result variable, do you know about it? Thanks for all your help until now!
  • NicholasMDS Oh boy... that SQL statement needs a lot of work.

    To start you need to specify table name to select values from.

    This will select all rows from table "names" and order them by Id

    $sql="SELECT * FROM names ORDER BY id";
    $results=mysql_query($sql);
    [/code:jtioblbm]
    
    But then how you use $results is also wrong. $results is a SQL object return by the statement. To get results from the SQL object you need to 
    [code:jtioblbm]
    $output = array();
    while($row = mysql_fetch_array($results, MYSQL_ASSOC)){
         $output[] = $row;
    }
    [/code:jtioblbm]
    
    $output is now an array representing the data from the table names. What you do with the data now is up to you. I don't really see what your trying to accomplish with this though?
Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)