sirine
sirine

Reputation: 527

How do I tell which radio button was selected in file HTML-Twig

I have 4 radio buttons in my code HTML:

<input id="spa-price" name="price" class="w3-radio" value="Spare {{ price.getSparePrice }}" type="radio">
<input id="spa-price" name="price" class="w3-radio" value="Repair {{ price.getRepairPrice }}" type="radio">
<input id="spa-price" name="price" class="w3-radio" value="Test {{ price.getTestPrice }}" type="radio">
<input id="spa-price" name="price" class="w3-radio" value="Std-Exchange {{ price.getExchangePrice }}" type="radio">

On post, I want to output a hidden field with the value of the selected radio button, eg:

<input type="hidden" name="lt" value="{{ price.getLt }}">

The code I have at the moment:

{% if $_POST['price'] == 'Spare' %}
<input type="hidden" name="lt" value="{{ price.getLt }}">

{% elseif $_POST['price'] == 'Repair' %} 
<input type="hidden" name="lt" value="{{ 10 }}">

{% elseif $_POST['price'] == 'Test' %} 
<input type="hidden" name="lt" value="{{ 10 }}">

{% else $_POST['price'] == 'Exchange' %} 
<input type="hidden" name="lt" value="{{ price.getLt }}">

Currently, this does not work.

What is my error ? Thank you.

Upvotes: 0

Views: 2072

Answers (1)

KevinTheGreat
KevinTheGreat

Reputation: 634

First, you have given every checkbox you made the same id that is bad practice. In HTML every id should have an unique name. I suggest you do the same for names. But for now that is optional

So change your id's to this:

<input id="spare-price" name="price" class="w3-radio" value="Spare {{ price.getSparePrice }}" type="radio">
<input id="repair-price" name="price" class="w3-radio" value="Repair {{ price.getRepairPrice }}" type="radio">
<input id="test-price" name="price" class="w3-radio" value="Test {{ price.getTestPrice }}" type="radio">
<input id="exchange-price" name="price" class="w3-radio" value="Std-Exchange {{ price.getExchangePrice }}" type="radio">

You can use Javascript to check if a box is checked

{% block javascripts%}
  <script>
        function validate(){
          var spare= document.getElementById('spare-price');
          if (spare.checked){
            alert("checked") ;
          }else{
            alert("You didn't check spare-price! Let me check it for you.")
          }
        }
     </script>
    {% endblock %}

Upvotes: 2

Related Questions