0 Favourites

How do I pass a global variable into AJAX request?

  • 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.

  • Something like this:

  • 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.

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

    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.

  • Construct 3

    Buy Construct 3

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

    Buy Now Construct 3 users don't see these ads
  • 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.

  • 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"

  • 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.

  • I have the same issue...can pass text all day, but not global variable set to text.

  • 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

  • simple php script

    <?php
    $user = $_GET['user']; 
    $phone = $_GET['phone'];
    $pass = $_GET['pass'];
    
    echo $user."  " .$phone. "  " .$pass ;
    exit;
    ?>[/code:huh8bnrh]
    
    [url=https://app.box.com/s/0wqm2gyani2d74fl8fbfnwq6ewpk4fte]ajaxPostData.capx[/url]
    
    [img="http://lookpic.com/O/i2/387/eXpujvL9.png"]
    [img="https://media.giphy.com/media/3otOKTMlPNQsx7IvLO/giphy.gif"]
    
    php script
    [code:huh8bnrh]<?php
    $user = $_POST['user']; 
    $phone = $_POST['phone'];
    $pass = $_POST['pass'];
    
    echo $user."  " .$phone. "  " .$pass ;
    exit;
    ?>[/code:huh8bnrh]
    
    [i][b]see the difference in PHP scripts $_GET['variable'] Vs $_POST['variable']........[/b][/i]
  • 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

  • Bump if someone have à capx example to GET_ And post msql to construct 2 plzzz

  • Bump if someone have à capx example to GET_ And post msql to construct 2 plzzz

    What else do you need? It's almost everything in this post.

    Other resources, with Google:

    how-do-i-use-php-in-construct-2_t62851

    https://www.scirra.com/tutorials/721/le ... ial/page-2

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