Posting string variables to a server using AJAX

Get help using Construct 2

Post » Mon Nov 18, 2013 6:44 am

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?rkatti2013-11-18 06:45:15
B
3
Posts: 7
Reputation: 189

Post » Mon Nov 18, 2013 8:10 am

How are you passing it to the server? Would be easier if you had an example to tell you what the issue might be.
B
20
S
6
G
4
Posts: 347
Reputation: 4,451

Post » Mon Nov 18, 2013 9:03 am

@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.
B
3
Posts: 7
Reputation: 189

Post » Mon Nov 18, 2013 9:14 am

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...
B
20
S
6
G
4
Posts: 347
Reputation: 4,451

Post » Mon Nov 18, 2013 9:51 am

@cvp

It didn't work. Is there any other reason it might not be passing?
B
3
Posts: 7
Reputation: 189

Post » Mon Nov 18, 2013 10:38 am

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.Paradox2013-11-18 10:40:07
B
239
S
62
G
33
Posts: 903
Reputation: 40,562

Post » Mon Nov 18, 2013 11:11 am

@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.
B
3
Posts: 7
Reputation: 189

Post » Mon Nov 18, 2013 3:57 pm

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 http://phppage.php?meh=some   throwing 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.
lennaert2013-11-18 15:59:04
Who dares wins
B
57
S
17
G
21
Posts: 1,878
Reputation: 19,572

Post » Mon Nov 18, 2013 5:52 pm

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)

[code]<?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.
?>[/code]
B
239
S
62
G
33
Posts: 903
Reputation: 40,562


Return to How do I....?

Who is online

Users browsing this forum: No registered users and 7 guests