M
M
Margomi2018-06-30 16:49:08
PHP
Margomi, 2018-06-30 16:49:08

How to display values ​​from identical form fields?

Hi everybody! The situation is this: the form is divided into several blocks and each block is shown depending on the selected value in the select. js select handler that changes blocks with display:none, display:block. This is an easy way that everyone knows. BUT!
There is a data entry field that participates in 3 blocks (1,2,3) the same and if you fill in block 1 and block 2, then the value from this field is not displayed, but only if it is filled in block 3. If this field is deleted from block 3, then the value will be displayed when block 2 is filled, and when block 1 is filled, this field will not be displayed.
That's actually the question: how to make the field value output from any block. not just the last one.
Displayed as standard

<?php if (!empty($а)): ?>
                    <li>
                      <strong><?php esc_html_e('Значение поля'); ?></strong>
                        <span><?php echo esc_html($а) ?></span>
                    </li>
                <?php endif; ?>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
alexalexes, 2018-06-30
@alexalexes

js select handler that changes blocks with display:none, display:block.

Option A. Rewrite this handler so that it definitely deletes the unused variant and creates a block with the required field variant, then you will not have extra fields in the form.
Option A2. Rewrite this handler so that in hidden blocks field names are prefixed, and not in hidden blocks, prefixes are removed, then the server will process only those fields that have normal names (without prefixes).
Variant A3. In addition to display: none, you need to disable fields with the disable attribute .
Option B. Fields with names that will be processed on the server should be put into separate hidden fields (there will be fields on the form with the same names, but with prefixes so that they do not intersect and are not taken into account by the server), and on submit, make a handler to he transferred values ​​to them according to the current state of the form.
PS: The most labor-intensive for the developer is option A3.
/* 
  Функция вкл и выкл полей формы в различных контейнерах
  @param str enable_id - id-контейнера, в котором нужно вкл. поля ввода
  @param arr disable_ids - массив id контейнеров, в которых нужно выкл. поля ввода
*/
function fields_enable_and_disable(enable_id, disable_ids)
{
  var enable_container = document.getElementById(enable_id); // получаем контейнер, в котором нужно "включить" поля ввода
  var field_types = ['input', 'select', 'textarea']; // все виды полей ввода с которыми нужно проделать манипуляцию вкл/выкл.
  var count_i = field_types.length; // кол-во типов полей
  for(var i = 0; i < count_i; i++) // перебираем типы полей
  {
    var field_type = field_types[i]; // текущий тип поля
    var enable_fields = enable_container.getElementsByTagName(field_type); // получаем коллекцию полей ввода, которые нужно вкл.
    var count_j = enable_fields.length; // кол-во полей текущего типа
    for(var j = 0; j < count_j; j++) // перебираем поля этого типа
      enable_fields[j].disabled = false; // включаем
    var count_k = disable_ids.length; // кол-во контейнеров, в которых нужно выкл. поля
    for(k = 0; k < count_k; k++) // перебираем неактивные контейнеры
    {
      var disable_id = disable_ids[k]; // id - текущего неактивного контейнера
      var disable_container = document.getElementById(disable_id); // получаем неактивный контейнер
      var disable_fields = disable_container.getElementsByTagName(field_type); // получаем поля текущего типа неактивного контейнера
      var count_n = disable_fields.length; // кол-во этих полей по текущ. типу
      for(var n = 0; l < count_n; n++)
        disable_fields[n].disabled = true; // выкл. эти поля
    }
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question