B
B
BEKa T2020-01-23 18:16:04
MySQL
BEKa T, 2020-01-23 18:16:04

How to get a large amount of data from inputs in a loop?

I don’t know which is better, loops or something else, but here’s what you need to do.
There are inputs ~ 207, they need to be sent via a socket to the backend and written from there to the database (also display with socket.emit the data that was received from the inputs on page in real time)
The template from which the data is sent

<table border="1" cellspacing="0" class="block">
    <caption><h1>Расписание на <span class="to-day"></span>
      <select name="" id="select-day">
        <option>Выберите день:</option>
        <option>Понедельник</option>
        <option>Вторник</option>
        <option>Среду</option>
        <option>Четверг</option>
        <option>Пятницу</option>
        <option>Субботу</option>
      </select>
    </caption>
    <tr>
      <th title="Урок"></th>
      <th title="Расписание звонков">Расп. звон.</th>
      <th colspan="2"><input title="Класс" placeholder="класс" /></th>
      <th colspan="2"><input title="Класс" placeholder="класс" /></th>
      <th colspan="2"><input title="Класс" placeholder="класс" /></th>
    </tr>

    <tr class="column1">
      <td>1</td>
      <td>08:00-09:10</td>
      <td><input type="text" placeholder="предмет" class="go"></td>
      <td><input class="cab cab-input gok" type="text" placeholder="каб." title="кабинет"></td>
      <td><input type="text" placeholder="предмет"></td>
      <td><input class="cab" type="text" placeholder="каб." title="кабинет"></td>
      <td><input type="text" placeholder="предмет"></td>
      <td><input class="cab" type="text" placeholder="каб." title="кабинет"></td>
    </tr>
  </table>
<script>
  $(function () {
    var socket = io();

    $('form').submit((e) => {
      var dataDay = '99';
      e.preventDefault();
      socket.emit('todo', dataDay);
    });
    });
</script>

5e29b80c57272819126990.png

As I said, there are more inputs than in the code above, there are about 207 of them

Backend
// Socket Connect
io.on('connection', (socket) => {
  console.log('Socket Run...')

  // Info to day
  socket.on('todo', (dataDay) => {
    
    // Query to DB
    let sql = `UPDATE dataDB SET body = '${dataDay}' WHERE dataDB.id = 1`;

    db.query(sql, (err, result) => {
      if (err) {
        throw err;
      } else {
        console.log('Day changed');
      }
    });

    io.emit('todo1', dataDay);
  });

  // Disconnect
  socket.on('disconnect', () => {
    console.log('Socket STOP!');
  });
});

The template where all these entries should appear
<table border="1" cellspacing="0" class="block">
  <caption><h1>Расписание на <span class="to-day">
    <b><%= data[0]['day'] %></b>
  </span>
</h1>
  </caption>
  <tr>
    <th title="Урок"></th>
    <th title="Расписание звонков">Расп. звон.</th>
    <% for (var i = 0; i < data.length; i++) { %>
      <th title="Класс" colspan="2"><%= data[i]['class'] %></th>
    <% } %>
  </tr>



  <tr>
    <td>1</td>
    <td>08:00-09:10</td>
    <% for (var i = 0; i < data.length && i < data.length; i++) { %>
      <td type="text" title="Алгебра"><%= data[i]['subject'] %></td>
      <td class="cab" type="text" title="кабинет"><%= data[i]['cabinet'] %></td>
    <% } %>
  </tr>
</table>

5e29b87608e26090685347.png
I wanted to do this: send the entire html code with inputs and write the whole html code to the database (and, as you already guessed, display this code on the page). But I could not get data from the inputs in html, and these inputs are displayed on the page, but I only wanted the text from the inputs.
I already made a chat according to the same principle, everything works well there, because there are only 1 inputs
PLEASE HELP I HAVE SUFFERED FOR A WEEK

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Leonid Knyazev, 2020-01-23
@amorphis

Read about FormData

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question