I need to grab market data from a javascript feed data and convert it to php variables to be later stored in data base.
Here is the javascript data feed code and php page:
` <script type="text/javascript" src="https://files.coinmarketcap.com/static/widget/currency.js">
</script>
<div class="coinmarketcap-currency-widget" data-currencyid="1" data-base="USDT" data-secondary="BTC" data-ticker="true" data-rank="true" data-marketcap="true" data-volume="true" data-stats="USD" data-statsticker="true">
if (window.XMLHttpRequest){
xmlhttp=new XMLHttpRequest();
}
else{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
var PageToSendTo = "sendprice.php?";
var MyVariable1 = "data-base";
var MyVariable2 = "data-secondary";
var MyVariable3 = "data-stats";
var MyVariable4 = "data-base";
var VariablePlaceholder = "btcprice";
var UrlToSend = sendprice.php? + data-base + btcprice;
xmlhttp.open("GET", UrlToSend, false);
xmlhttp.send();
<?php
$btcprice = $_GET['btcprice'];
?>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="content-type" content="text/html" />
<meta name="author" content="Portal Associates" />
<title>Untitled 3</title>
</head>
<body>
<?php
echo $btcprice;
?>
</body>
</html>
`
I want to be able to capture realtime data and store it in db for chart generation. I am not a javascript programmer. Please enlighten me.
Thanks in advance,
Batoe
Try this:
JavaScript:
var MyVariable1 = "data-base";
var MyVariable2 = "data-secondary";
var MyVariable3 = "data-stats";
var MyVariable4 = "data-base";
var VariablePlaceholder = "btcprice";
var UrlToSend = "sendprice.php";
var params = VariablePlaceholder + '=' + MyVariable1 + '&MyVariable2=' + MyVariable2 + '&MyVariable3=' + MyVariable3 + '&MyVariable4=' + MyVariable4;
xmlhttp.open( "GET", UrlToSend + '?' + params, false );
xmlhttp.send();
PHP (in sendprice.php):
$btcprice = $_GET['btcprice'];
$MyVariable2 = $_GET['MyVariable2'];
$MyVariable3 = $_GET['MyVariable3'];
$MyVariable4 = $_GET['MyVariable4'];
Related
This is a part of my code:
function addText(id, type){
alert("ID="+id);
alert("TYPE="+type);
var title = getData("title", id);
var text = getData("text", id);
var imageUrl = getData("imageUrl", id);
document.getElementById("contentArea").innerHTML += '<div id="' + id + '"> </div>';
if(type == 1){
allText = html1.responseText;
titleReplace = "K" + id + "title";
textReplace = "K" + id + "text";
imageReplace = "K" + id + "image";
allText.replace("K1title", titleReplace);
allText.replace("K1text", textReplace);
allText.replace("K1image", imageReplace);
}
document.getElementById(id).innerHTML = allText;
document.getElementById(titleReplace).innerHTML = title;
document.getElementById(textReplace).innerHTML = text;
document.getElementById(imageReplace).src = imageUrl;
}
function getData(Parameter, id){
var result = undefined;
var URL = phpGetURL + "?par=" + Parameter + "&id=" + id;
var xmlhttp = new XMLHttpRequest();
xmlhttp.open('GET', URL ,false);
xmlhttp.send();
result = xmlhttp.responseText;
xmlhttp.abort();
//lert(result);
return result;
}
And this is a part from the index.php where is call the function 4 times:
<script type="text/javascript" src="js/dataManager.js"></script>
<script type="text/javascript"> addText(1, 1); addText(2, 1); addText(3, 1); addText(4, 1); </script>
The first one displays, the second one displays but not with the replaced text. The fourth and the fifth doesn't display at all.
It's gets the data from a php script that gets it from a MYSQL database.
I don't get it to work.
thx
I had the same issue while working on an extension for chrome.
You should may try to initialize the "XMLHttpRequest()"-instance at the very beginning of your code, so that is only created once.
I'm posting this for others, because i've read that you had to solve this problem fast, years ago... So it is not urgent anymore i guess...
Can anyone tell me why I cannot seem to sort this table? I downloaded sorttable.js from this site: http://www.kryogenix.org/code/browser/sorttable/#ajaxtables
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html">
<title>XML Table Sorting</title>
<link rel="stylesheet" type="text/css" media="all" href="css/styles.css">
<script type="text/javascript" src="js/jquery-1.10.2.min.js"></script>
<script type="text/javascript" src="js/jquery.tablesorter.min.js"></script>
<script type="text/javascript" src="jssort.js"></script>
</head>
<body>
<div id="wrapper">
<h1>Sortable Table of Search Queries</h1>
<table id="sortableTable" class="sortable" cellspacing="0" cellpadding="0" ></table>
</div>
<script>
// load page and display table data
r(function()
{
alert('DOM Ready!');
loadXMLDoc();
sortTheTable();
});
function r(f){/in/.test(document.readyState)?setTimeout('r('+f+')',9):f()}
function loadXMLDoc()
{
if (window.ActiveXObject)
{
var xmlDoc = new ActiveXObject('Microsoft.XMLDOM');
xmlDoc.async = 'false';
xmlDoc.load('Linux_Sort.xml');
}
else
{
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function()
{
if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
{
createTable(xmlhttp);
}
};
xmlhttp.open("GET", "Linux_Sort.xml", false);
xmlhttp.send();
} // end else
}
function createTable(xml)
{
var i;
var xmlDoc = xml.responseXML;
var tableInsert = '<thead><tr><th id="problem_header"><span>Problem Type</span></th><th><span>Task</span></th><th id="page_header"><span>Page No.</span></th><th id="risk_header"><span>Risk</span></th></tr></thead>';
tableInsert +='<tbody>';
var x = xmlDoc.getElementsByTagName("record");
for (i = 0; i <x.length; i++)
{
tableInsert += '<tr><td class="lalign">' +
x[i].getElementsByTagName("actionplan")[0].childNodes[0].nodeValue +
'</td><td class="lalign">' +
x[i].getElementsByTagName("task")[0].childNodes[0].nodeValue +
"</td><td>" +
x[i].getElementsByTagName("pageNo")[0].childNodes[0].nodeValue +
"</td><td class='risk'>" +
x[i].getElementsByTagName("risk")[0].childNodes[0].nodeValue +
"</td></tr>";
}
tableInsert += '</tbody>';
document.getElementById("sortableTable").innerHTML = tableInsert;
// document.getElementByTagName("tbody").innerHTML = tableInsert;
}
function sortTheTable()
{
var newTableObject = document.getElementById("sortableTable")
sorttable.makeSortable(newTableObject);
}
</script>
</body>
</html>
Here is the sample XML:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Linux xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<record>
<actionplan> OAv </actionplan>
<task>something here</task>
<pageNo>6</pageNo>
<risk>3</risk>
</record>
<record>
<actionplan>Op</actionplan>
<task>something here</task>
<pageNo>7</pageNo>
<risk>3</risk>
</record>
<record>
<actionplan>Op</actionplan>
<task>something here</task>
<pageNo>9</pageNo>
<risk>2</risk>
</record>
<record>
<actionplan>Op</actionplan>
<task>something here</task>
<pageNo>10</pageNo>
<risk>2</risk>
</record>
<record>
<actionplan>Op</actionplan>
<task>Disal1 root login via SSH</task>
<pageNo>12</pageNo>
<risk>1</risk>
</record>
<record>
<actionplan>Op</actionplan>
<task>Disable send and accept ICMP redirects</task>
<pageNo>13</pageNo>
<risk>1</risk>
</record>
</Linux>
I have the table piece working. however, I cannot seem to get it to sort. It is relevant to note that this information is being displayed via local files for use on a tablet browser. So far, it only displays in Firefox.
Put the tbody out of the loop:
var tableInsert = '<thead><tr><th id="problem_header"><span>Problem Type</span></th><th><span>Task</span></th><th id="page_header"><span>Page No.</span></th><th id="risk_header"><span>Risk</span></th></tr></thead>';
tableInsert +='<tbody>';
var x = xmlDoc.getElementsByTagName("record");
for (i = 0; i <x.length; i++)
{
tableInsert += '<tr><td class="lalign">' +
x[i].getElementsByTagName("actionplan")[0].childNodes[0].nodeValue +
'</td><td class="lalign">' +
x[i].getElementsByTagName("task")[0].childNodes[0].nodeValue +
"</td><td>" +
x[i].getElementsByTagName("pageNo")[0].childNodes[0].nodeValue +
"</td><td class='risk'>" +
x[i].getElementsByTagName("risk")[0].childNodes[0].nodeValue +
"</td></tr>";
}
tableInsert += '</tbody>';
also don't forget double-quote around the id:
var newTableObject = document.getElementById("sortableTable")
As the ActiveXObject has no method to check readyState, a messy method is to call sortTable after using a setTimeout function
<html>
<head>
</head>
<body>
<div id="content"></div>
<script>
function handleResponse(response) {
for (var i = 0; i < response.items.length; i++) {
var item = response.items[i];
// in production code, item.text should have the HTML entities escaped.
document.getElementById("content").innerHTML += "<br>" +item.volumeInfo.title+"<br>"+item.volumeInfo.imageLinks.thumbnail;
}
}
</script>
<script src="https://www.googleapis.com/books/v1/volumes/buc0AAAAMAAJ"></script>
</body>
</html>
Here I am Trying to Get Details of The Book Through Specific ID from the Google Books Please Help in JavaScript and JSON
You can get the information about a particular book by making xhr request. I have created a function which return title and thumbanil of particular Book ID:
function getBookDetails(id) {
var xmlhttp = new XMLHttpRequest();
var url = "https://www.googleapis.com/books/v1/volumes/" + id;
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
var item = JSON.parse(xmlhttp.responseText);
document.getElementById("content").innerHTML += "<br>" + item.volumeInfo.title + "<br>" + "<img src=\""+item.volumeInfo.imageLinks.thumbnail+"\" >";
}
}
xmlhttp.open("GET", url, true);
xmlhttp.send();
}
getBookDetails("buc0AAAAMAAJ");
Working Fiddle
I see this type of JS codes on so many website for Google adsense ad units.
<!DOCTYPE HTML>
<html>
<head>
<title>Adsense Test</title>
</head>
<body>
<script type="text/javascript">
google_ad_type = 'text';
google_gl = 'default';
google_kw = 'vps';
google_kw_type = 'broad';
google_ad_client = 'ca-xxxxxxxxxxxxxxxx';
google_ad_output = 'js';
google_adtest = 'on';
google_feedback = 'on';
google_language = 'en';
google_encoding = 'utf8';
google_max_num_ads = '20';
function google_ad_request_done(google_ads)
{
var s = '';
var i;
for (i=0;i<google_ads.length;++i)
{
s += '<div class="gad-slot"><pre>';
s += '<strong>'+google_ads[i].line1+'</strong><br />';
s += google_ads[i].line2+ '<br />' + google_ads[i].line3+'<br />';
s += '<small>' + google_ads[i].visible_url + '</small></pre></div>';
}
document.write(s);
return;
}
</script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
</body>
</html>
I want also use this type of code on my site.
Please tell me is legal or illegal for Google adsense policy.
you can't use this code because it is for premium publisher only
https://www.google.com/adsense/premium-login
I am trying to grab any xml tag in this sample. But if I try grabbing anything other than the data in the first record in IE I cannot do it. What kind of javascript can I write to correct this?
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Get Any XML Tag</title>
<script type='text/javascript' src='http://imaginationeverywhere.info/jslib//dev/jquery-1.5.1.js'>
</script>
<script type="text/ecmascript">
function getData() {
var XMLHTTPRequestObject = false;
if (window.XMLHTTPRequest) {
XMLHTTPRequestObject = new XMLHttpRequest();
} else if (window.ActiveXObject) {
XMLHTTPRequestObject = new ActiveXObject('Microsoft.XMLHTTP');
}
if (XMLHTTPRequestObject) {
XMLHTTPRequestObject.open('GET', 'http://imaginationeverywhere.info/xml/cd_catalog.xml', true);
XMLHTTPRequestObject.onreadystatechange = function () {
if (XMLHTTPRequestObject.readyState == 4 && XMLHTTPRequestObject.status == 200) {
var xmlDocument = XMLHTTPRequestObject.responseXML;
displayArtist(xmlDocument);
}
}
XMLHTTPRequestObject.send(null);
}
}
function displayArtist(xmldoc) {
titleName = xmldoc.getElementsByTagName('TITLE');
artistName = xmldoc.getElementsByTagName('ARTIST');
countryName = xmldoc.getElementsByTagName('COUNTRY');
companyName = xmldoc.getElementsByTagName('COMPANY');
priceName = xmldoc.getElementsByTagName('PRICE');
yearName = xmldoc.getElementsByTagName('YEAR');
displayTitle = "The name of this song title is " + titleName[0].firstChild.nodeValue + ".";
displayArtist = "The name of the artist is " + artistName[0].firstChild.nodeValue + ".";
displayCountry = "The name of the artist country is " + countryName[0].firstChild.nodeValue + ".";
displayCompany = "The name of the artist company is " + companyName[0].firstChild.nodeValue + ".";
displayPrice = "This song costs $" + priceName[0].firstChild.nodeValue + ".";
displayYear= "The year this song was released " + yearName[0].firstChild.nodeValue + ".";
var target = document.getElementById('targetDiv');
target.innerHTML = displayTitle + "<br/>" + displayArtist + "<br/>" + displayCountry + "<br/>" + displayCompany + "<br/>" + displayPrice + "<br/>" + displayYear;
}
</script>
</head>
<body>
<h3>Get Tag Value</h3>
<form action="#">
<input type="button" value='Get the name of the artist' onclick="getData()" />
</form>
<div id="targetDiv" style="width:300px; height:20px">
</div>
</body>
</html>
I'd suggest using jQuery - it's practically all browser compatible.
Try this:
function getData() {
$.ajax({
url: 'ajax/test.html',
dataType : 'xml',
success: function(data) {
displayArtist(data);
});
}