t3cho
t3cho

Reputation: 375

Read which button was clicked to variable

How to read to variable which button is clicked.

I have this five button's which are like answers.

<fieldset> <legend> Question1 </legend>
   <input type="button" value="1st Ansswer"/>
   <input type="button" value="2nd Ansswer"/>
   <input type="button" value="3rd Ansswer"/>
   <input type="button" value="4th Ansswer"/>
   <input type="button" value="5th Ansswer"/>
</fieldset>

I want to read the answer for question 1 into variable to send it via email.

For better explain i want something like this This is for

<select name="SOption"><option>Option1</option><option>Option2</option></select>

I read the result in variable Question like this

$Question= $_POST['SOption'];

How to do the same with button's instead select.

Hope you understand

Upvotes: 0

Views: 58

Answers (3)

t3cho
t3cho

Reputation: 375

I managed to do this with hiden button

function change_value($id, $value)
            {
                document.getElementById($id).value = $value;
            }


                    <input type='button' class="myButton2" onclick="change_value('quest1', this.value);" value="Ans1">
                    <input type='button' class="myButton3" onclick="change_value('quest1', this.value);" value="Ans2">
                    <input type='button' class="myButton4" onclick="change_value('quest1', this.value);" value="Ans3">
                    <input type='button' class="myButton5" onclick="change_value('quest1', this.value);"value="Ans4">

Upvotes: 0

Kylie
Kylie

Reputation: 11749

Why cant you just use radio buttons??

 <fieldset> <legend> Question1 </legend>
   <input type="radio" name="radio_q1" value="1st Ansswer"/>
   <input type="radio" name="radio_q1" value="2nd Ansswer"/>
   <input type="radio" name="radio_q1" value="3rd Ansswer"/>
   <input type="radio" name="radio_q1" value="4th Ansswer"/>
   <input type="radio" name="radio_q1" value="5th Ansswer"/>
 </fieldset>
 <fieldset> <legend> Question2 </legend>
   <input type="radio" name="radio_q2" value="1st Ansswer"/>
   <input type="radio" name="radio_q2" value="2nd Ansswer"/>
   <input type="radio" name="radio_q2" value="3rd Ansswer"/>
   <input type="radio" name="radio_q2" value="4th Ansswer"/>
   <input type="radio" name="radio_q2" value="5th Ansswer"/>
 </fieldset>

 <input type="submit" value="SUBMIT">

Otherwise another way would be with a hidden text field, and javscript/jquery. Like so....

 <form id='theForm' method='POST' action='wherever'>
 <input type='hidden' name='thequestion1' id='thequestion1'>
 </form>

 <button class='questionButton' data-answer='TheAnswer 1'>Answer 1</button>
 <button class='questionButton' data-answer='TheAnswer 2'>Answer 2 </button>
 <button class='questionButton' data-answer='TheAnswer 3'>Answer 3 </button>
 <button class='questionButton' data-answer='TheAnswer 4'>Answer 4 </button>

Then your jquery..

 $(document).ready,function(){

   $('.questionButton').click(function(e){
      e.preventDefault();
      var answer = $(this).data('answer');
      $('#thequestion1').val(answer);
      $('#theForm').submit();
   });

 });

And then from this, you can just extrapolate how you would do multiple questions, store them all in the hidden text fields for each question, then submit the form in the end.

Upvotes: 0

Eric Lagergren
Eric Lagergren

Reputation: 511

<fieldset> 
    <legend> 
        Question1 
    </legend>
    <input type="button" name="foo" value="1st Ansswer"/>
</fieldset>

 $Question= $_POST['foo'];

Just add a name or ID to each button whose value you want.

Upvotes: 1

Related Questions