I have trouble with getting a value from an xmlhttp request after sending it with ajax to a php file and insert it into an mysql database. I get from the yahoo finance api the output you can see in the html snippet. After that the value of this html element should be sended to the file insertvolume.php.
After successful sending I save the data into an variable named $jsonString and decode it. Then I try to insert a specific value from the array into my mysql database but it wont work. I think the problem is that the value is into any other arrays but I dont know how to write that. I only need that array named results Any hints?
the html:
<div id="output">{"query{"count":1,"created":"20160215T09:15:04Z","lang":"deDE","results":{"quote":{"symbol":"ZN","Ask":"2.05","LastTradeRealtimeWithTime":null,"ChangePercentRealime":null,"ChangeFromYearHigh":"-0.45","LastTradeWithTime":"4:00pm<b>1.81</b>",astTradePriceOnly":"1.81", "Volume":"500","HighLimit":null,"LowLimit":null,"DaysRange":"1.781"}}}}</div>
javascript:
var outputt = $('#output').text();
$.ajax({
type: "POST",
dataType: "json",
url: "insertvolume.php",
data: {myData: outputt},
success: function(data){
//alert('Items added');
}
});
some pice of code from insertvolume php:
$jsonString = $_POST['mydata'];
$jsonArray = json_decode($jsonString, true);
$jsonArray1 = $jsonArray['query']['results']['quote'];
if ($stmt = $mysqli->prepare('INSERT INTO volume ( stocksymbol, volume, time) VALUES ( ?, ?, now())')) {
/* bind parameters for markers */
$stmt->bind_param($jsonArray1['symbol'], $jsonArray1['volume']);
/* execute query */
$stmt->execute();
/* close statement */
$stmt->close();
}
<div id="output">{"query{"count":1,"created":"20160215T09:15:04Z","lang":"deDE","results":{"quote":{"symbol":"ZN","Ask":"2.05","LastTradeRealtimeWithTime":null,"ChangePercentRealime":null,"ChangeFromYearHigh":"-0.45","LastTradeWithTime":"4:00pm<b>1.81</b>",astTradePriceOnly":"1.81","HighLimit":null,"LowLimit":null,"DaysRange":"1.781}}}}</div>