0

I know this is basic but i don't know what else i can do. So i need your help. Ill tell you what happened.

The story is that i have a database request and I want to json encode the data from that request and use it through a javascript file ($.ajax or $.json).

the data i need from that request are two values. Latitude and longitude. but when i receive the json_encode from php and put it in the console log or an alert it displays undefined instead of the "double precision"(because the json string).

Ill show you the code.

the php

    <?php
$dbconn3 = pg_connect
    ("host= localhost port=5432 dbname=EmergenciesResponse    user=postgres password=asdf");                


    $query1=pg_query
    ("select latitude, longitude from emergency where   id_emergency=5");

    $arreglo=array();
              while($reg=pg_fetch_assoc($query1)){
               $arreglo[]=$reg;

             }

              echo json_encode($arreglo);  

             pg_close($dbconn3);

              ?>

Then the .js code

 $.ajax({                                      
          url: 'consulta2.php',                  
          data:{
                },                        
          method: 'POST',                               
          dataType:'json',                
          success: function(data)          
          {                   
              alert("latitude:"+data.latitude+"longitude:"+data.longitude);        


          }
        });

As I said when I do an alert instruction the output prints undefined but the string of the php is correct. Sorry if its very easy but I don't know what to do about the undefined value

thx for your time.

0

2 Answers 2

2

Your PHP returns an array of objects. The response will look like this (without pretty print):

[
    {
        "latitude": 0.000,
        "longitude": 0.000
    },
    {
        "latitude": 1.000,
        "longitude": 2.000
    },
    {
        "latitude": 3.000,
        "longitude": 4.000
    }
]

However, in JS you are treating is as an object.

You would understand what the problem is if you simply opened your consulta2.php file and looked at the response.

There can be two causes:

  1. id_emergency is not unique. Then, your PHP is good, but in JS you need to iterate through the array:

    $.ajax({                                      
        url: 'consulta2.php',                  
        data:{
        },                        
        method: 'POST',                               
        dataType:'json',                
        success: function(data)          
        {  
            $.each(data, function() { // <-------
                    alert("latitude:"+this.latitude+"longitude:"+this.longitude);
            });
        }
    });
    

    You will get from 0 to many alerts, one for every pair of coordinates.

  2. id_emergency is unique. Then, your JS is good, but in PHP you do not need to form an array. It should be as simple as:

    $query1=pg_query("select latitude, longitude from emergency where   id_emergency=5");
    
    //$arreglo=array(); // don't need an array
    
    $reg = pg_fetch_assoc($query1); // only 1 object
    echo json_encode($reg); // encode the object, but not an array
    
    pg_close($dbconn3);    
    

    As this is the single object, you will get only one alert.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you a lot, this really helped me. Now I have to figure out how to make the same idea for multiple data. Greetings from chile
0

I think there is a problem in your while loop, I think you are overwriting the same index in the loop, you should be increment the index at each iteration.

var counter = 0;
while($reg=pg_fetch_assoc($query1)){
    $arreglo[counter]=$reg;
    counter++;
}

3 Comments

No, it's ok. $arreglo[] will push the item into the array.
Please, read this StackOverflow topic: stackoverflow.com/questions/676677/…
@YeldarKurmangaliyev Didn't know that. Thanks for the link :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.