Answer the question
In order to leave comments, you need to log in
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);
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
<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
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question