How do I pass a global variable into AJAX request?

Get help using Construct 2

Post » Sat Apr 18, 2015 6:34 pm

I'm trying to figure out how to pass my global variable "UserID" into an AJAX request to that I can post another global variable "Score" into the correct user table data. The global variable "UserID" is already set on the log in layout and should match up with the user id my the mysql database. So far, everything else is working. I can login and call up data from my table, but I'm hung up on how to update my database with player scores.
B
10
S
5
G
4
Posts: 23
Reputation: 2,923

Post » Sat Apr 18, 2015 10:05 pm

Something like this:
Image
B
55
S
29
G
19
Posts: 1,520
Reputation: 25,670

Post » Sun Apr 19, 2015 2:07 am

Thank you for the response. I have that set up correctly in C2 (I think). The problem may just be in my php. I took a screencap of my code minus passwords ect.

Image

Just incase it's a problem I'm causing in C2, here is what I have:

Image

Sorry... I am so lost. I can't figure out what I'm doing wrong. I'm really rusty in php/ mysql and have little experience with AJAX.

Oddly my login script works fine and I can even print out my scores, usernames and ids into a text field with no issue. I'm using an entirely different code for the score however. The entire thing is a mishmash from scattered tutorials as none of them seem current.
B
10
S
5
G
4
Posts: 23
Reputation: 2,923

Post » Sun Apr 19, 2015 2:46 am

Your script uses GET requests, @ramones uses POST in the screenshot.
So make sure they use the same; either "POST" + $_POST or "GET" & $_GET

And in C2 screenshot the user id variable name is user_id and in the php script it is 'uid', make sure these are the same as well.
B
32
S
7
G
2
Posts: 317
Reputation: 2,851

Post » Sun Apr 19, 2015 3:00 am

They are the same and I've tried it with both POST and GET. It still doesn't update the mysql table with the new score. My global variable in C2 is called "UserID"
B
10
S
5
G
4
Posts: 23
Reputation: 2,923

Post » Sun Apr 19, 2015 6:21 am

To test if your php side code works, use the $_GET[] setup in your php script, and add echo mysql_error(); below the mysql query update, and then manually visit your page.

ie open the url in a browser,

http://yoursite/score_send.php?uid=1&score=10




Also, you do a mysql_select_db("dbname", $con) referring to your database name as dbname ... which Im pretty sure is not the actual name of your database.

If you applied the above, the second issue should be displayed in your browser as: error database "dbname" does not exist.
Who dares wins
B
57
S
17
G
21
Posts: 1,878
Reputation: 19,572

Post » Mon Oct 10, 2016 7:15 pm

I have the same issue...can pass text all day, but not global variable set to text.
B
11
S
2
Posts: 38
Reputation: 661

Post » Tue Oct 11, 2016 9:04 am

@jsutton: this is a year old topic, so consider providing more informations about your specific issue.
The best would probably to post your capx so other members from the community can investigate it.

The example in ramones' post is the valid way to go though:
"Some text" & The_Variable & " Some more text" & The_Other_Variable
New to Construct ? Where to start

Image Image

Image Image

Please attach a capx to any help request or bug report !
Moderator
B
293
S
117
G
96
Posts: 7,310
Reputation: 71,069

Post » Tue Oct 11, 2016 10:31 am

Image
Image

simple php script
Code: Select all
<?php
$user = $_GET['user'];
$phone = $_GET['phone'];
$pass = $_GET['pass'];

echo $user."  " .$phone. "  " .$pass ;
exit;
?>



ajaxPostData.capx


Image
Image

php script
Code: Select all
<?php
$user = $_POST['user'];
$phone = $_POST['phone'];
$pass = $_POST['pass'];

echo $user."  " .$phone. "  " .$pass ;
exit;
?>



see the difference in PHP scripts $_GET['variable'] Vs $_POST['variable']........
B
67
S
24
G
7
Posts: 1,518
Reputation: 11,072

Post » Tue Feb 21, 2017 1:49 pm

Hi all i Work on this Way to have good request for add msql var to c2 var. Korbaach i go tout test your exemple thank you so much for this. This is very important for me to resolve this problèm. If someone Know more of this solution to read server data im here for read you
B
11
S
1
Posts: 28
Reputation: 1,963

Next

Return to How do I....?

Who is online

Users browsing this forum: SEGamestudio and 6 guests