How do I Make an Ajax URL

Get help using Construct 2

Post » Tue May 10, 2016 12:41 am

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: Select all
[code]"http://WebsiteForTestting com/Control/TheAPi.php?name="&Variable1[/code]


and all i get back are the default players details.

any pointers please ?
B
13
S
4
Posts: 60
Reputation: 1,001

Post » Tue May 10, 2016 2:02 am

Not without more information, are you trying to load player data from your server? What does your php file look like (omitting sensitive information)?
Nothing goes unanswered!
B
24
S
5
G
1
Posts: 434
Reputation: 3,446

Post » Tue May 10, 2016 2:31 am

briggybros wrote: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,


Code: Select all
<?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."|";
    }
?>
B
13
S
4
Posts: 60
Reputation: 1,001

Post » Thu May 12, 2016 12:14 pm

I'm no expert at php, so I could be horribly wrong, but I think that this
Code: Select all
if (isset($_GET['user'])) {$user = $_GET['user'];} else {$user = $_POST["name"];}
if (isset($_GET['count'])) {$count = $_GET['count'];} else {$count = 1;}


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?
Nothing goes unanswered!
B
24
S
5
G
1
Posts: 434
Reputation: 3,446

Post » Mon May 16, 2016 9:55 am

briggybros wrote:I'm no expert at php, so I could be horribly wrong, but I think that this
Code: Select all
if (isset($_GET['user'])) {$user = $_GET['user'];} else {$user = $_POST["name"];}
if (isset($_GET['count'])) {$count = $_GET['count'];} else {$count = 1;}


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,
Code: Select all
<form action="TheAPi.php" method="post">
Name: <input type="text" name="name"><br>

<input type="submit">
</form>

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: Select all
"http://WebsiteForTestting com/Control/TheAPi.php?name="&Variable1
B
13
S
4
Posts: 60
Reputation: 1,001

Post » Mon May 16, 2016 7:20 pm

@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.
Nothing goes unanswered!
B
24
S
5
G
1
Posts: 434
Reputation: 3,446

Post » Wed May 18, 2016 3:34 am

briggybros wrote:@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.
B
13
S
4
Posts: 60
Reputation: 1,001

Post » Wed May 18, 2016 11:42 am

Kniggles wrote: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.

C2 Manual wrote: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.

GET - Requests data from a specified resource
POST - Submits data to be processed to a specified resource
B
66
S
24
G
7
Posts: 1,515
Reputation: 11,041

Post » Wed May 18, 2016 12:19 pm

This is what you've got right?

Image

which is equivalent to this:

Image

But what I think you're wanting is this:

Image

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:

Image

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

Code: Select all
if (isset($_GET['user'])) {$user = urldecode($_GET['user']);} else {$user = urldecode($_POST["name"]);}
Nothing goes unanswered!
B
24
S
5
G
1
Posts: 434
Reputation: 3,446

Post » Thu May 19, 2016 3:08 am

briggybros wrote:This is what you've got right?

Image

which is equivalent to this:

Image

But what I think you're wanting is this:

Image

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:

Image

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

Code: Select all
if (isset($_GET['user'])) {$user = urldecode($_GET['user']);} else {$user = urldecode($_POST["name"]);}

These Both work Thank you very Much :) :D :D :D :D :D :D :D
B
13
S
4
Posts: 60
Reputation: 1,001

Next

Return to How do I....?

Who is online

Users browsing this forum: Euduardo, Kin and 30 guests