php – There are 2 select in the form, you need to send the value of only one in POST

Question:

The task is 2 lists (data is taken from the database from the 1st table with a list of topics and a description with a unique id), which are switched depending on the selected button (not necessarily input radio), and the value of only the displayed list is sent to POST.

The code:

<input type="radio" checked="checked" name="cheker" id="cheker1"/>
    <span>Заявка </span>
<input type="radio" name="cheker" id="cheker2"/>
    <span>Возврат </span>

<div class="select vozvrat">
    <select name="theme">
        {foreach name=questions from=$questions key=id item=theme}
            {if $theme.group == '1'};
                <option value="{$theme.id}">{$theme.theme_text}</option>
            {/if}
        {/foreach}
    </select>
</div>
<div class="select zayavka">
    <select name="theme">
        {foreach name=questions from=$questions key=id item=theme}
            {if $theme.group == '2'};
                <option value="{$theme.id}">{$theme.theme_text}</option>
            {/if}
        {/foreach}
    </select>
</div>

css radio buttons:

#cheker2:checked ~ .zayavka {display: none;}

#cheker1:checked ~ .vozvrat {display: none;}

The lists are displayed depending on the selected radio button.

When submitting a form in POST, regardless of the selected value in any of the lists, the first value {$ theme.id} from the second select is sent.

{$ theme.group} just groups the values ​​from the table into 1 and 2 groups.

In other words, first the value of the first list is written to theme, and then it is immediately rewritten from the second.

Throw a link to an example with 2 selections, or hint how to score correctly in Google))

Answer:

You will not find a correct example of a solution with your code. Your layout was initially incorrectly built, there should not be two elements with the same name within the same form, but you have exactly two elements with the same name theme . There can be two solutions here:

  1. You change the names of the elements and change your code accordingly for this case, both on the front and in the backend.
  2. You change the front code so that you only have one element with the specified name at a time. This can be implemented without hiding / showing the desired element by the value of another, but by adding / removing them to the page code (but here you cannot do with pure css ). Thus, nothing will overwrite anything, and from a standards point of view, browsers will have no complaints about your code, since there will not be two elements with the same name in the form at once.
Scroll to Top
AllEscort