How do I Make an Ajax URL

0 favourites
  • 11 posts
From the Asset Store
A Single-Player with Multiple Avatars "En Gard RPG" online tool with hostile encounters and local adventures.
  • Hi I am trying to post " name = Variable1 " to a php file on my server,

    The Php file TheAPi.php works with a simple html post it button, and it also works if I use the form as an Iframe on the layout

    the line i have been working on gos like

    AJAX

    URL=

    [code]"http://WebsiteForTestting com/Control/TheAPi.php?name="&Variable1

    [/code:3npaib2w]

    and all i get back are the default players details.

    any pointers please ?

  • Not without more information, are you trying to load player data from your server? What does your php file look like (omitting sensitive information)?

  • Not without more information, are you trying to load player data from your server? What does your php file look like (omitting sensitive information)?

    Hi briggybros,

    <?php
    require_once('TwitterAPIExchange.php');
    /** Set access tokens here - see: https://dev.twitter.com/apps/ **/
    include 'crystals.php';
    $url = "https://api.twitter.com/1.1/statuses/user_timeline.json";
    $requestMethod = "GET";
    if (isset($_GET['user'])) {$user = $_GET['user'];} else {$user = $_POST["name"];}
    if (isset($_GET['count'])) {$count = $_GET['count'];} else {$count = 1;}
    $getfield = "?screen_name=$user&count=$count";
    $twitter = new TwitterAPIExchange($settings);
    $string = json_decode($twitter->setGetfield($getfield)
    ->buildOauth($url, $requestMethod)
    ->performRequest(),$assoc = TRUE);
    if($string["errors"][0]["message"] != "") {echo "<h3>Sorry, there was a problem.</h3><p>Twitter returned the following error message:</p><p><em>".$string[errors][0]["message"]."</em></p>";exit();}
    foreach($string as $items)
        {
            $Alpha = $items['user']['profile_banner_url'];
            $Beta = $items['user']['profile_image_url'];
            $Ceta = $items['user']['name'];
            $Delta = $items['user']['screen_name'];
            $Ebco = $items['user']['description'];
            echo "". $Alpha."|";
            echo "". $Beta."|";
            echo "". $Ceta."|";
            echo "". $Delta."|";
            echo "". $Ebco."|";
        }
    ?>
    [/code:1na4cb9b]
  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • I'm no expert at php, so I could be horribly wrong, but I think that this

    if (isset($_GET['user'])) {$user = $_GET['user'];} else {$user = $_POST["name"];}
    if (isset($_GET['count'])) {$count = $_GET['count'];} else {$count = 1;}
    [/code:3spmx9rm]
    
    is where your problem lies for two reasons.
    
    1. Your AJAX request is passing the variable name as a http get variable. However, you are testing whether the get variable called user is set, which it never is. Therefore, it's entering the else block which is setting the user variable to the post variable called name; which it seems would also not be present as you are passing the variable as a get. 
    
    2. This might just be a lacking of understanding of how scoping in php works, but the $user and $count variables are being created in conditional blocks. When these blocks are exited are these variables still in scope?
  • I'm no expert at php, so I could be horribly wrong, but I think that this

    > if (isset($_GET['user'])) {$user = $_GET['user'];} else {$user = $_POST["name"];}
    if (isset($_GET['count'])) {$count = $_GET['count'];} else {$count = 1;}
    [/code:hcyw75s0]
    
    is where your problem lies for two reasons.
    
    1. Your AJAX request is passing the variable name as a http get variable. However, you are testing whether the get variable called user is set, which it never is. Therefore, it's entering the else block which is setting the user variable to the post variable called name; which it seems would also not be present as you are passing the variable as a get. 
    
    2. This might just be a lacking of understanding of how scoping in php works, but the $user and $count variables are being created in conditional blocks. When these blocks are exited are these variables still in scope?
    

    if I use this code to send the request, From a page with just this button on,

    <form action="TheAPi.php" method="post">
    Name: <input type="text" name="name"><br>
    
    <input type="submit">
    </form>
    [/code:hcyw75s0]
    From a page the output is what i want.
    So i got it down to not sending the post from Construct to the PHP.
    This line i feel is wrong , just not sure how though 
    [code:hcyw75s0]"http://WebsiteForTestting com/Control/TheAPi.php?name="&Variable1[/code:hcyw75s0]
  • Kniggles The page with the button on it is using the http POST protocol to pass the form data with variable name 'name' to the php script. The second code segment is passing the variable 'name' to the php script via the http GET protocol. These are different so they are accessed differently by php. You can either change the request to a post request in Construct or change the php script to recognize the GET variable name as well as the post variable.

  • Kniggles The page with the button on it is using the http POST protocol to pass the form data with variable name 'name' to the php script. The second code segment is passing the variable 'name' to the php script via the http GET protocol. These are different so they are accessed differently by php. You can either change the request to a post request in Construct or change the php script to recognize the GET variable name as well as the post variable.

    I do not understand,

    The Ajax settings in construct2 are

    Tag

    ""

    URL

    ""

    Data

    ""

    Method

    "POST"

    I can not see another others i can change and this is already set to POST.

  • I do not understand,

    The Ajax settings in construct2 are

    ....

    Method

    "POST"

    I can not see another others i can change and this is already set to POST.

    Post to URL

    Send a request with data to a URL and retrieve the response. A tag is provided to match it up with the On completed, On progress and On error triggers. Construct 2 does not automatically URL encode the post data - use the URLEncode system expression to ensure the data is in the correct format for posting. Note post data is in the same format as a query string, e.g. "foo=1&bar=2".

    The method can also be specified: by default it is POST, but for some APIs you may need to change this to GET,PUT, DELETE,HEAD or another HTTP method.

    [quote:171anj5f]GET - Requests data from a specified resource

    POST - Submits data to be processed to a specified resource

  • This is what you've got right?

    which is equivalent to this:

    But what I think you're wanting is this:

    of course URL encoding the message if there is any chance there is a special character in there at all. URLEncode is a system expression. So you'll probably finish with this:

    But then you need to make sure you decode within the php script so the variable assignment lines will look more like:

    if (isset($_GET['user'])) {$user = urldecode($_GET['user']);} else {$user = urldecode($_POST["name"]);}
    [/code:u6ebrxp9]
  • This is what you've got right?

    which is equivalent to this:

    But what I think you're wanting is this:

    of course URL encoding the message if there is any chance there is a special character in there at all. URLEncode is a system expression. So you'll probably finish with this:

    But then you need to make sure you decode within the php script so the variable assignment lines will look more like:

    > if (isset($_GET['user'])) {$user = urldecode($_GET['user']);} else {$user = urldecode($_POST["name"]);}
    [/code:14tgp1dp]
    

    These Both work Thank you very Much

  • >

    > I do not understand,

    > The Ajax settings in construct2 are

    > ....

    > Method

    > "POST"

    >

    > I can not see another others i can change and this is already set to POST.

    >

    > Post to URL

    > Send a request with data to a URL and retrieve the response. A tag is provided to match it up with the On completed, On progress and On error triggers. Construct 2 does not automatically URL encode the post data - use the URLEncode system expression to ensure the data is in the correct format for posting. Note post data is in the same format as a query string, e.g. "foo=1&bar=2".

    > The method can also be specified: by default it is POST, but for some APIs you may need to change this to GET,PUT, DELETE,HEAD or another HTTP method.

    >

    > [quote:1qcv5vpu]GET - Requests data from a specified resource

    > POST - Submits data to be processed to a specified resource

    >

    Thank you for helping

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