0

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","resu‌​lts":{"quote":{"symbol":"ZN","Ask":"2.05","LastTradeRealtimeWithTime":null,"Chang‌​ePercentRealime":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();
    }
Bodoppels
  • 406
  • 7
  • 24
  • I can't tell if that JSON is not valid, or it got messed up when you pasted it in. As is, there are line breaks in it though. – Tomanow Feb 15 '16 at 10:17
  • I made these line breaks for better reading but I can delete them. – Bodoppels Feb 15 '16 at 10:20
  • 1
    Is your html ouput is like this : `
    {"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:00pm1.81",astTradePriceOnly":"1.81","HighLimit":null,"LowLimit":null,"DaysRange":"1.781}}}}
    `
    – Alankar More Feb 15 '16 at 10:30

3 Answers3

2

After decoding the data you cannot retrive the json data directly. We should create object for that. Here i am giving you the reference link of how to access values of json.

Get value from JSON array in PHP

Community
  • 1
  • 1
  • Can I decode it like that: ´$jsonArray = json_decode($jsonString, true);´ and the into mysql instad of ´$jsonArray1['volume']´ this: ´$query->results[0]->{"quote"}->Volume´? I dont understand the rules/ syntax when select the specific value of this:´$query->results[0]->{"quote"}->Volume´ is it right like that? – Bodoppels Feb 15 '16 at 11:53
1

In javascript code it is written as var outputt = $( "#output" ).val();

But the div is not having the value. The content is present inside the div. So change the line code to

var outputt = document.getElementById('output').textContent and try.

Now you are able to access the array in javascript. But we are unable to access the data of output code from your line.

1

In ajax call remove the line

contentType: "application/json; charset=utf-8",

and then execute. It will work