1

I am working on my college project, In that i have a webpage which is supposed to display City and temperature, I am trying to use AJAX to update temperature at some intervals. Here's the code

for ajax request Java Script, I saw this code in this answer https://stackoverflow.com/a/18243161/4341530

function postAjaxRequest(qry){
alert("Ajax response on " +qry);
$.ajax({
type: "POST",
url: "ajax_resp.php",
data: {q: qry},
dataType:'JSON', 
success: function(response){
    dispatchCallBack(qry,response);
}});}

the alert("Ajax response on " +qry); is working fine so I know this code is running. But when I put this kind of "alert()" just before dispatchCallBack(qry,response); its not working, so I infer here that the request is not successful.

Here's ajax_resp.php

<?php

if(isset($_POST['q'])&&!empty($_POST['q'])
{
    if($_POST['q']=="c_temp")
    {
        $api_key = "Here's my api key, working alright"; 
        $city_id = "here's city id, also working correct";
        $url = "http://api.openweathermap.org/data/2.5/forecast/city?id=".$city_id."&APPID=".$api_key;

        $json = file_get_contents($url);
        $json_o = json_decode($json);

        $city_name =  $json_o->city->name;
        $temp_c =  $json_o->list[0]->main->temp-273;
        echo json_encode(array("city"=>$city_name,"temp"=>$temp));
//I have checked JSON response seperately its correct. I was able to get city name and temperature variable properly initialised.
    }
}?>

so I can't figure out what's wrong. I am newbie :p May be some silly mistake, would be greatfull if pointed out.

Community
  • 1
  • 1
Salim
  • 143
  • 11
  • 1
    add `.catch` callback and see if it fails – Max Koretskyi Oct 14 '16 at 18:13
  • Have you watched the AJAX request / response in the browser's developer tools? Have you included the jQuery library in the project? Are there any errors reported? Are you running this on a web-server? – Jay Blanchard Oct 14 '16 at 18:18
  • begin by opening the browser's developer tools, watch the network activity. this will let you know if the issue is on the client side or the server. if it's a server issue then you'll see the error response. if it's a client then no request will come out at all. – Nir Oct 14 '16 at 18:19
  • 1
    this `$_POST[q]` should be `$_POST['q']` – Jeff Oct 14 '16 at 18:21
  • and you'd need to assign `$qry= $_POST['q'];` first – Jeff Oct 14 '16 at 18:22
  • OK, thanks for mentioning, I've changed it to : `if(isset($_POST['q'])&&!empty($_POST['q'])` and `if($_POST['q']=="c_temp")` though still not solves the problem – Salim Oct 14 '16 at 18:26
  • Do yourself a favor, simply place a `print_r($_POST)` or `var_dump($_POST)` in the PHP page which receives the form submission. Fill out your form, submit and look closely in the developer tools to make sure you have actually sent what you expect. – Jay Blanchard Oct 14 '16 at 18:36

1 Answers1

0

Please look at the code below and correct them

[1] use single quotes or double quotes to get data from $_POST variable having the string index

[2] $qry is not defined earlier. After reading the question the most probable value of the $qry can be $_POST['q']. So replace $qry with $_POST['q'] or assign $qry = $_POST['q']

if(isset($_POST['q'])&&!empty($_POST['q'])
{
if($_POST['q']=="c_temp") <==  $_POST['q'] instead of $qry
{
    $api_key = "Here's my api key, working alright"; 
    $city_id = "here's city id, also working correct";
    $url = "http://api.openweathermap.org/data/2.5/forecast/city?id=".$city_id."&APPID=".$api_key;

    $json = file_get_contents($url);
    $json_o = json_decode($json);

    $city_name =  $json_o->city->name;
    $temp_c =  $json_o->list[0]->main->temp-273;
    echo json_encode(array("city"=>$city_name,"temp"=>$temp));
    //I have checked JSON response seperately its correct. I was able to get city name and temperature variable properly initialised.
}
}?>
Naresh Kumar
  • 561
  • 2
  • 15
  • Why should the OP try this? A ***good answer*** will always have an explanation of what was done and why it was done in such a manner, not only for the OP but for future visitors to SO. – Jay Blanchard Oct 14 '16 at 18:24
  • @JayBlanchard does that sound good ? – Naresh Kumar Oct 14 '16 at 18:29
  • Ok, I've made changes, I think I was planning to use $qry but missed it, anyways, Still the same, any other suggestions, anything else wrong with the code ? – Salim Oct 14 '16 at 18:31
  • @Salim write `console.log(response)` to see what is returned from the php file – Naresh Kumar Oct 14 '16 at 18:34
  • Actually if I delete all the php code and just hard code `echo json_encode(array("city"=>"London","temp"=>32));` then too it doesn't works – Salim Oct 14 '16 at 18:35
  • @Salim var_dump($_POST) to see what you are sending to the page and use console.log(response) in the success function to see what you get from the php page – Naresh Kumar Oct 14 '16 at 18:39