Quantcast
Channel: Active questions tagged return-value - Stack Overflow
Viewing all articles
Browse latest Browse all 207

Why does my array keep returning "Undefined array key"

$
0
0

I want to retrieve the api data from timezone api by clicking the submit button, for the first table row. Unfortunetly it is showing an error message whenever I visit the url link http://localhost/task/geonamesExample/libs/php/getTimezone.php?lang=en&country=GB. to see the error (even when I try it without going to the link).

I expect it to return a value of latitude once I click on the submit, and it's suppose to display in the last row.

I tried deleting geonames a couple, I've tried using var_dump($decode).

Moreover this is the error message I keep getting
Warning: Undefined array key "geonames" in /Applications/XAMPP/xamppfiles/htdocs/task/geonamesExample/libs/php/getTimezone.php on line 25
{"status":{"code":"200","name":"ok","description":"success","returnedIn":"98 ms"},"data":null}

index.html:

<!doctype html><html lang="en"><head><link rel="stylesheet" href="style.css"><meta charset="utf-8"><title>AJAX/PHP/CURL/JSON example</title><meta name="description" content="AJAX/PHP/CURL/JSON example"><meta name="author" content="Paddy Evans"></head><body><select id="selCountry"><option value="GB">Great Britain</option><option value="FR">France</option><option value="DE">Germany</option><option value="US">USA</option></select><select id="selLanguage"><option value="en">English</option><option value="fr">Francais</option><option value="de">Deutsche</option></select><button id="btnRun">Run</button><br><br><div id="divResults"><table><tr><td>                            API name</td><td>                            API description</td><td></td></tr><tr><td>                            1.PostalCodeLookup</td><td>                                    Timezone:</td><td><select id="selCountry2"><option value="47.01">47.01</option></select></td><td><button id="btnRun_2">Submit</button></td></tr><tr><td>                            2.Weather</td><td>                            description</td><td><button type="submit">Submit</button></td></tr><tr><td></td><td>                            description</td><td><button type="submit">Submit</button></td></tr><tr><td align="right">                            Continent: </td><td id="txtContinent"></td></tr><tr><td align="right">                            Capital: </td><td id="txtCapital"></td></tr><tr><td align="right">                            Languages: </td><td id="txtLanguages"></td></tr><tr><td align="right">                            Population: </td><td id="txtPopulation"></td></tr><tr><td align="right">                            Area (km<sup>2</sup>): </td><td id="txtArea"></td></tr><tr><td align="right">                            Latitude:</td><td id="txtLatitude"></td></tr></table></div><script type="application/javascript" src="libs/js/jquery-2.2.3.min.js"></script><script type="application/javascript" src="libs/js/script.js"></script></body></html>

script.js:

    $('#btnRun').click(function() {            $.ajax({                url: "libs/php/getCountryInfo.php",                 type: 'POST',                dataType: 'json',                data: {                    country: $('#selCountry').val(),                    lang: $('#selLanguage').val()                },                success: function(result) {                    console.log(JSON.stringify(result));                    if (result.status.name == "ok") {                        $('#txtContinent').html(result['data'][0]['continent']);                        $('#txtCapital').html(result['data'][0]['capital']);                        $('#txtLanguages').html(result['data'][0]['languages']);                        $('#txtPopulation').html(result['data'][0]['population']);                        $('#txtArea').html(result['data'][0]['areaInSqKm']);                    }                },                error: function(jqXHR, textStatus, errorThrown) {                    // your error code                }            });         });        $('#btnRun_2').click(function() {            $.ajax({                url: "libs/php/getTimeZone.php",                type: 'POST',                dataType: 'json',                data: {                    country: $('#selCountry2').val(),                    lang: $('#selLanguage2').val()                },                success: function(result) {                    console.log(JSON.stringify(result));                    if (result.status.name == "ok") {                        $('#txtLatitude').html(result['data'][0]['lat']);                        $('#txtLongitude').html(result['data'][0]['lng']);                        $('#Timezone').html(result['data'][0]['timezone']);                }                },                error: function(jqXHR, textStatus, errorThrown) {                    // your error code                }            });        });

getTimezone.php

<?php    ini_set('display_errors', 'On');    error_reporting(E_ALL);    $executionStartTime = microtime(true);    $url='http://api.geonames.org/timezoneJSON?formatted=true&lat=' . $_REQUEST['lang'] . '&country=' . $_REQUEST['country'] . '&username=adeyinka_25';    $ch = curl_init();        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);        curl_setopt($ch, CURLOPT_URL,$url);        $result=curl_exec($ch);        curl_close($ch);        $decode = json_decode($result,true);            $output['status']['code'] = "200";        $output['status']['name'] = "ok";        $output['status']['description'] = "success";        $output['status']['returnedIn'] = intval((microtime(true) - $executionStartTime) * 1000) . " ms";        $output['data'] = $decode['geonames'];        header('Content-Type: application/json; charset=UTF-8');        echo json_encode($output);    ?>

Viewing all articles
Browse latest Browse all 207

Trending Articles