1

I have the following PHP code on countries.php.

$stmt = $pdo->prepare("SELECT id, name FROM countries");
$stmt-> execute();

$data = array();
while($row = $stmt->fetch()){
  $data[] = array(
    'id' => $row['id'],
    'name' => $row['name']
  );
}

echo json_encode($data);

And following JS in onboard.js

$.ajax({
  url: "processes/countries.php",
  type: 'GET',
  dataType: 'json',
  success: function(countriesList) {
    var questions = [
      {
        question: "What's your country?", 
        type: "select", 
        // response : ['India', 'America', 'Japan']
        response: $.each(countriesList, function(){ 
          this.name; // DISPLAYS [object Object]
        })
      }
    ]
  }
});

As you can see I am trying to get the list of countries and display it. However, countriesList in jquery code here returns [object Object]. How can I get the response to print the countries names?

enter image description here

Network tab response shows data is returned in the following manner:

[
  {"id":"1","name":"Afghanistan"},
  {"id":"2","name":"Albania"},
  {"id":"3","name":"Algeria"},
  {"id":"4","name":"American Samoa"},
]

The data given above is taken after switching to the RAW format.

Relaxing Music
  • 452
  • 4
  • 13
  • Does this answer your question? [Print content of JavaScript object?](https://stackoverflow.com/questions/1625208/print-content-of-javascript-object) – luk2302 Jul 17 '21 at 12:40
  • Change to console.log(countriesList); – mplungjan Jul 17 '21 at 12:42
  • @mplungjan the actual problem is in response part of the questions array and not in the alert. – Relaxing Music Jul 17 '21 at 14:27
  • What is `questions[0].response` supposed to be? – Val Kornea Jul 17 '21 at 16:10
  • @VladimirKornea List of countries in select box.. I have updated a sample response in the question. Please check for the commented response part. – Relaxing Music Jul 17 '21 at 16:25
  • I mean what do you expect `console.log(questions[0].response)` to print? – Val Kornea Jul 17 '21 at 16:54
  • @VladimirKornea If you would have read the question well you would have understood it. Anyways, thanks for your time. I really appreciate it. And do checkout accepted answer by Rinshan Kolayil. – Relaxing Music Jul 17 '21 at 16:56
  • The problem WAS the alert since the toString of your object was not useful. Using console.log, you will get a better representation and you would see the structure of the returned object – mplungjan Jul 17 '21 at 17:24

1 Answers1

2
var questions = [{
    question: "What's your country?",
    type: "select",
    response: countriesList.map(function(elem) {
        return elem.name;
    })
    // response: countriesList.map(function(elem) {
    //     return '<option value="'+elem.name+'">'+elem.name+'</option>';
    // }).join("")
}]
Rinshan Kolayil
  • 1,111
  • 1
  • 9
  • 14
  • If you wish to have names of country as array, can you please try above code, i updated. Or can you please tell which structure you with to see the output – Rinshan Kolayil Jul 17 '21 at 16:47
  • 1
    Perfect.. That's what I was looking for all this time.. So that's how it works. Thanks man! Saved my day :) – Relaxing Music Jul 17 '21 at 16:53
  • 1
    For your answer update, I actually only needed what you gave earlier. The `commented` part is not required. I am defining the ` – Relaxing Music Jul 17 '21 at 16:58