Home  >  Q&A  >  body text

How to merge two fields with the same name but different IDs into the same row in MySQL?

I have a project where the customer can select the price based on the checkbox and the input box has the same name, If I select the checkbox but have no input, I only want to store the value of the checkbox in the database, if I select the input box I only want to store the value of the input box. This is my code:

<ul class="donate-now">[enter image description here](https://i.stack.imgur.com/EyU7O.png)
                  <li>
                    <input type="radio" class="form-control btn-default target" id="a100" name="amount" value="100"/>
                    <label for="a25">100€/mois</label>
                  </li>
                  <li>
                    <input type="radio" class="form-control btn-default target" id="a200" name="amount" value="200" />
                    <label for="a50">200€/mois</label>
                  </li>
                  <li>
                    <input type="radio" class="form-control btn-default target" id="a300" name="amount" value="300" checked/>
                    <label for="a75">300€/mois</label>
                  </li>
                 
                </ul>
                </div>
              
            
                
                <div class="container form-control-lg" id="montant-libre">
                  <span class="input-symbol-euro"><input type="text" class="form-control form-control-md" style="color: #d34607;" id="montantlibre" onclick="clearRadio()"  placeholder="Montant libre" name="amount" min="1"/></span>
                  
                </div>

Do I need to make some changes on my mysql since I keep getting errors?

P粉087951442P粉087951442178 days ago296

reply all(1)I'll reply

  • P粉419164700

    P粉4191647002024-04-05 10:51:24

    <form id="donate">
       <ul class="donate-now">
          <li>
             <input type="radio" class="form-control btn-default target" id="a100" name="amount" value="100"/>
             <label for="a25">100€/mois</label>
          </li>
          <li>
             <input type="radio" class="form-control btn-default target" id="a200" name="amount" value="200" />
             <label for="a50">200€/mois</label>
          </li>
          <li>
             <input type="radio" class="form-control btn-default target" id="a300" name="amount" value="300" checked/>
             <label for="a75">300€/mois</label>
          </li>
       </ul>
       <div class="container form-control-lg" id="montant-libre">
          <span class="input-symbol-euro">
          <input type="text" class="form-control form-control-md" style="color: #d34607;" id="montantlibre" onclick="clearRadio()"  placeholder="Montant libre" name="amount" min="1"/>
          </span>
       </div>
    </form>
    <script>
       // 获取表单元素
       var form = document.getElementById("donate");
       if (form) {
       // 为表单添加提交事件监听器
       form.addEventListener("submit", function(event) {
         // 获取选中的单选按钮
         var selectedRadio = document.querySelector('input[name="amount"]:checked');
       
         // 获取输入字段
         var inputField = document.getElementById("montantlibre");
         var amount = selectedRadio.value;
         // 检查输入字段是否有值
         if (!Number(inputField.value) !== true) {
           // 新值
            amount = inputField.value;
         }
       
         console.log(amount);
         // 阻止默认的表单提交
         event.preventDefault();
       });
       }
    </script>
    
    

    Details:

    1-Use document.getElementById("donate") to get the form element.

    2-Use form.addEventListener("submit", function(event) { ... });Add a submit event listener to the form.

    3-Inside the event listener function, use document.querySelector('input[name="amount"]:checked') to get the selected radio button.

    4-Use document.getElementById("montantlibre") to get the input field.

    5-Use if (!Number(inputField.value) !== true) { ... }Check whether the input field has a value.

    6-If the input field has a value, store that value in the amount variable. Otherwise, store the value of the selected radio button in the amount variable.

    7-Use event.preventDefault() to prevent default form submission.

    reply
    0
  • Cancelreply