Posting string variables to a server using AJAX

0 favourites
  • 9 posts
From the Asset Store
Color Variable
$4.50 USD
10% off
Easily store, modify, read and manipulate colors with Color Variables!
  • I have a global string variable whose output is something like:

    265|53|232|etc. etc.

    I need to pass this variable to a server using AJAX and PHP and store it in a MYSQL database. Text variables are getting stored in the server but this isn't. Any ideas why?

  • How are you passing it to the server? Would be easier if you had an example to tell you what the issue might be.

  • cvp

    dl.dropboxusercontent.com/u/88263565/record.capx is the capx file

    dl.dropboxusercontent.com/u/88263565/savescores.php is the php file we're using to pass it to mysql.

  • I think the problem is your '|' character

    I am not totally sure, but it wouldnt suprise me if that messed it up.

    So try running it with 'A' as a seperator to see if it works. Or "escape" the '|' character in your string.

    Hope it helps...

  • cvp

    It didn't work. Is there any other reason it might not be passing?

  • Did you try entering the URL manually, and see which response the PHP shows?

    I've used the high score save myself, so I know the | character works.

    I just manually entered my get script url for my Pucked game and it sent:

    Paradox|16496|Glitz|13431|Joseph|7240|Joseph|6978|ABR|5681|A|5667|ABRA|5607|b|5254|ABR|5029|A|4708|

    edit:

    Just had a thought, is your database expecting a number for that spot, or a string? If it's formatted for a number and your sending strings, that could be the problem.

  • Paradox

    could you post an example of your php file cause it's not working when I enter it manually either. I'm guessing it has to be something to do with the mysql syntax in the php file.

  • Extend your php trigger, with $_GET[variable] for the time being.

    ie

    if($_POST[meh] == "some")

    with

    if($_POST[meh] == "some" or $_GET[meh] == "some")

    From there you can stuff your variables in a url phppage.php that in a browser would result in the same results your ajax call receives, but then on screen.

    Also enable displaying errors

    error_reporting(E_ALL)   <- willl show lots of progamming errors.

    Also, after each mysql query type: echo mysl_error();

    if you then check the results page through a manual action, it shows whats wrong with the mysql, if any.

    Also, you could copy the mysql query, go to php myadmin, and perform a manual sql assignment. It could display errors in your query.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Here's mine you can compare, (not real names) but I have a feeling it may be the database.

    This PHP deletes them after they are 10 days old, but only when a new score is added. (I need to change that eventually)

    <?php 
     
    $db = "puckdata";//Your database name 
    $dbu = "serv_pucked";//Your database username 
    $dbp = "fakename";//Your database users' password 
    $host = "localhost";//MySQL server - usually localhost 
     
    $dblink = mysql_connect($host,$dbu,$dbp); 
    $seldb = mysql_select_db($db); 
     
    if(isset($_GET['name']) && isset($_GET['score'])){ 
     
         //Lightly sanitize the GET's to prevent SQL injections and possible XSS attacks 
         $name = strip_tags(mysql_real_escape_string($_GET['name'])); 
         $score = strip_tags(mysql_real_escape_string($_GET['score'])); 
      $xtime = date(U); 
      $time = time() - (60 * 60 * 24 * 7); 
         $sql = mysql_query("INSERT INTO `$db`.`scores` (`id`,`name`,`score`,`xdate`) VALUES ('','$name','$score',now());"); 
     
     
       
         if($sql){ 
     
              //The query returned true - now do whatever you like here. 
              echo 'Your score was saved. Congrats!'; 
     
         }else{ 
     
              //The query returned false - you might want to put some sort of error reporting here. Even logging the error to a text file is fine. 
              echo 'There was a problem saving your score. Please try again later.'; 
     
         } 
      echo $time; 
      $qry_del = mysql_query("DELETE FROM `$db`.`scores` WHERE xdate < DATE_SUB(NOW(), INTERVAL 10 DAY)"); 
      if($qry_del){ 
     
              //The query returned true - now do whatever you like here. 
              echo 'Old Scores Deleted'; 
     
         }else{ 
     
              //The query returned false - you might want to put some sort of error reporting here. Even logging the error to a text file is fine. 
              echo 'There was a problem deleting the old scores.'; 
     
         }     
    }else{ 
         echo 'Your name or score wasnt passed in the request. Make sure you add ?name=NAME_HERE&score=1337 to the tags.'; 
    } 
     
    mysql_close($dblink);//Close off the MySQL connection to save resources. 
    ?>
Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)