There is a popup with a chat bot and one input . When you open the window immediately there is a message from the bot, then the user builds a dialogue. After each submission, it is necessary to take an array with user messages and send an array consisting of all messages by json.
At the moment, the ability to send each message to the server separately is implemented.
Code example:
<div id="chatbot"> <div id="chatbot-message"> <p> <span class="user">bot: </span> Здравствуйте! Как Вас зовут? </p> <br> <p class="from-user"><span class="user">Вы: </span>msg1</p><br> <p class="from-bot"><span class="user">bot: </span>bot msg</p><br> <p class="from-user"><span class="user">Вы: </span>msg2</p><br> <p class="from-bot"><span class="user">bot: </span>bot msg</p><br> <p class="from-user"><span class="user">Вы: </span>msg3</p><br> <p class="from-bot"><span class="user">bot: </span>bot msg</p></div> <form action="ajax.php" method="post" id="chatform"> <div class="input-group"> <input id="chatbot-input" type="text" name="msg" class="form-control" placeholder="Введите сообщение..." autocomplete="off" required=""> <div class="input-group-btn"> <input onclick="AjaxFormRequest('chatform', 'ajax.php')" id="chatbot-submit" class="btn btn-default" type="submit" value=""> </div> </div><!-- .input-group --> </form> var messages = document.getElementsByClassName("from-user"); //for(var i=0; i < messages.length; i++) { var msgs = messages[i].innerHTML; } //var msg1 = messages[0]; //var msg2 = messages[1].innerHTML; function AjaxFormRequest(formData, url) { console.log(messages[0].innerHTML); // console.log(msg1); // console.log(msg2); jQuery.ajax({ url: url, type: "POST", dataType: "html", data: jQuery("#" + formData).serialize(), }); } Do not judge strictly, I'm just learning. Thanks in advance for your advice.