L
L
less_junior2022-02-16 21:03:42
JavaScript
less_junior, 2022-02-16 21:03:42

How to output required data from json string to div when requesting php?

Making a request from a form:

<form action="input1_save.php" method="post">
    <input type="text" name="num" value="2">
    <input type="text" name="id" value="32">
    <input type="submit" name="" value="go">
</form>
<div id="out">
    <div id="name"></div>
    <div id="f_name"></div>
    <div id="enable"></div>
</div>


json arrives in response:

[
    {
        "conf":{
            "type":"text",
            "tech_type":"js",
            "parser":"jq",
            "comm1":"5mr",
            "comm2":"priv",
            "name":"Petr",
            "f_name":"Ivanov",
            "enable":"no"
        }
    }
]


It is necessary that the values ​​from the list are displayed in the div id according to their name.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rst0, 2022-02-17
@Rst0

HTML

<div class="out"></div>
 <!-- пустой div для вывода данных-->

JS
// в скобках JSON.parse() ответ сервера json
const answer = JSON.parse('[{"conf":{"type":"text","tech_type":"js","parser":"jq","comm1":"5mr","comm2":"priv","name":"Petr","f_name":"Ivanov","enable":"no"}}]')[0].conf;  

const out = document.querySelector('.out'); // находим div 

out.innerHTML =''; // очищаем его 
for (key in answer) {    //перебираем объект 
      out.innerHTML += '<div id="'+key+'">'+answer[key]+'</div>'; добавляем внутрь out 

}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question