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?
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.