You are viewing our Forum Archives. To view or take place in current topics click here.
JavaScript help required
Posted:
JavaScript help requiredPosted:
Status: Offline
Joined: Nov 28, 20149Year Member
Posts: 21
Reputation Power: 0
So I'm trying to have the result that's received from sending a HTTP GET request to an HTTP API set as the result of a div. How can I go about loading the result from the API?
#2. Posted:
Status: Offline
Joined: Mar 04, 201014Year Member
Posts: 2,891
Reputation Power: 150
I would do an XMLHttpRequest. You could also use cURL in PHP, but if your wanting to achieve this in JavaScript, use this script.
Now all you need in the html is a way of calling the method (say on the click of a button or onload), and the div id of whatever you call in the code above.
function someFunctionToLoadStuff()
{
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("content").innerHTML=xmlhttp.responseText; //change content to anything you want.
}
}
var url = "file";
xmlhttp.open("POST",url,true);
xmlhttp.send();
}
Now all you need in the html is a way of calling the method (say on the click of a button or onload), and the div id of whatever you call in the code above.
- 1useful
- 0not useful
#3. Posted:
Status: Offline
Joined: Nov 28, 20149Year Member
Posts: 21
Reputation Power: 0
Hey, I can't seem to get that to work with the following code.
var xmlhttp;
function someFunctionToLoadStuff()
{
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("result").innerHTML=xmlhttp.responseText; //change content to anything
}
}
function listener()
{
var url = "http://www.somewebsite.com/?tool=skyperesolver&username=live:dean.hemmingfield";
xmlhttp.open("POST",url,true);
xmlhttp.send();
someFunctionToLoadStuff;
}
- 0useful
- 0not useful
#4. Posted:
Status: Offline
Joined: Mar 04, 201014Year Member
Posts: 2,891
Reputation Power: 150
Status: Offline
Joined: Mar 04, 201014Year Member
Posts: 2,891
Reputation Power: 150
Endermite wrote Hey, I can't seem to get that to work with the following code.
var xmlhttp;
function someFunctionToLoadStuff()
{
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("result").innerHTML=xmlhttp.responseText; //change content to anything
}
}
function listener()
{
var url = "http://www.somewebsite.com/?tool=skyperesolver&username=live:dean.hemmingfield";
xmlhttp.open("POST",url,true);
xmlhttp.send();
someFunctionToLoadStuff;
}
My apologies. I thought you were trying to achieve this from a file on your own server.
To grab it from another URL, use cURL in PHP. Here is an example:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Grab Test</title>
</head>
<body>
<h2>Grab Stuff</h2>
<?php
if(isset($_POST['submitBtn'])) {
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, 'http://pastebin.com/raw.php?i=fkNDcc7i');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);
echo $data;
}
?>
<form action="" method="POST">
<input type="submit" name="submitBtn" value="Click to load" />
</form>
</body>
</html>
- 0useful
- 0not useful
#5. Posted:
Status: Offline
Joined: Nov 28, 20149Year Member
Posts: 21
Reputation Power: 0
Right, thank you for your help, now how can I add a value from a HTML TextField to the url to load in PhP?
- 0useful
- 0not useful
#6. Posted:
Status: Offline
Joined: Mar 04, 201014Year Member
Posts: 2,891
Reputation Power: 150
Status: Offline
Joined: Mar 04, 201014Year Member
Posts: 2,891
Reputation Power: 150
Endermite wrote Right, thank you for your help, now how can I add a value from a HTML TextField to the url to load in PhP?
Use this
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Grab Test</title>
</head>
<body>
<h2>Grab Stuff</h2>
<?php
if(isset($_POST['submitBtn'])) {
$ch = curl_init();
$timeout = 5;
$query = $_POST['itemToSearch'];
curl_setopt($ch, CURLOPT_URL, 'http://thetechgame.com/raw.php?i={$query}');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);
echo $data;
}
?>
<form action="" method="POST">
<input type="text" name="itemToSearch" placeholder="something" />
<input type="submit" name="submitBtn" value="Click to load" />
</form>
</body>
</html>
- 0useful
- 0not useful
#7. Posted:
Status: Offline
Joined: Mar 28, 20159Year Member
Posts: 39
Reputation Power: 1
#8. Posted:
Status: Offline
Joined: Mar 04, 201014Year Member
Posts: 2,891
Reputation Power: 150
Status: Offline
Joined: Mar 04, 201014Year Member
Posts: 2,891
Reputation Power: 150
Gander wrote Hey man,
I was on Work Experiance with some Coders who work for santander called Isban, who develops programs and the website for them etc..
They use this for fun --- [ Register or Signin to view external links. ]
Refrain from using W3Schools. It's basically the 'Wikipedia for Web'
I would use the Mozilla Development Network [ Register or Signin to view external links. ] for front-end, and for any PHP-related problems, search up the function on PHP's Documentation site: [ Register or Signin to view external links. ]
- 0useful
- 0not useful
You are viewing our Forum Archives. To view or take place in current topics click here.