0

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

t3cho
  • 365
  • 1
  • 6
  • 18

3 Answers3

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

Eric Lagergren
  • 501
  • 2
  • 7
  • 18
  • Wait i want to know which answer is clicked . I have question with 5 posible answers. I want to know which answer user clicked . – t3cho Jun 24 '14 at 19:48
  • @t3cho then give them all unique names and have unique PHP variables for each button? – Eric Lagergren Jun 24 '14 at 19:52
  • Yes but that wont help me. I'm sorry because i didn't explain well . Imagine it like this. You have a question and 5 answers. What is your favorite color . button1 Blue button2 Red button3 Green ... Now when you answer the question i want to get the result. How can i know which answer you choose ? I want to store it to $YourAnswer – t3cho Jun 24 '14 at 19:56
  • Oh, okay, then I'll refer you to this: http://stackoverflow.com/q/2680160 and this: http://stackoverflow.com/q/5669052 it's gonna be a little more complex than your original example. – Eric Lagergren Jun 24 '14 at 19:59
0

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.

Kylie
  • 11,421
  • 11
  • 47
  • 78
0

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">
t3cho
  • 365
  • 1
  • 6
  • 18