H
H
haa2015-08-26 16:18:02
PHP
haa, 2015-08-26 16:18:02

How to correctly pass data by json_encode?

Good day. The question is how to transfer data from the database using json_encode? I extract the data like this.

$data = array();
  $ads = $db->query('SELECT name, id FROM ads WHERE ads_id=?i', $_POST['id']);
  while($ads2 = $ads->fetch_array()){
    $data[] = $ads2;
  }	
       echo json_encode($data);

js processing like this
var ads = JSON.parse(xmlhttp.responseText); // Преобразуем JSON-строку в массив
var text = "<option value=''>Текст</option>"; // Начинаем создавать элементы в select
for (var i in ads) {
/* Перебираем все элемены и создаём набор options */
text += "<option value='"+i+"'>" + ads[i] + "</option>";
}
document.myform.cities.innerHTML = text; // Устанавливаем options в select


But it gives me [object Object]
<option value="0">[object Object]</option>
<option value="1">[object Object]</option>
<option value="2">[object Object]</option>
<option value="3">[object Object]</option>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Oleg, 2015-08-26
@haa

try like this:
in general, try this:
console.log(ads);
and see what you get.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question