3 Replies - 274 Views - Last Post: 28 June 2014 - 01:59 AM

#1 t3cho  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 90
  • Joined: 27-May 14

How to know which button was clicked.

Posted 25 June 2014 - 06:30 AM

I have a question and 5button's as posible answers.

This looks like this

Posted Image

When someone click on one of those button's it open's the new page with a new question.

How can i know which answer someone clicked to send it via email .

I know how to do it with radio, or select input type. How to do it with button's ?

<fieldset> <legend> Question1 </legend>
<input type="submit" name="b1" value="1st Ansswer"/>
<input type="submit" name="b2" value="2nd Ansswer"/>
<input type="submit" name="b3" value="3rd Ansswer"/>
<input type="submit" name="b4" value="4th Ansswer"/>
<input type="submit" name="b5" value="5th Ansswer"/>
</fieldset>
<input type="submit" name="Send" value="Send results"/>	


php part

if($_POST['Send']) {
		$ime = 'DreaminCode';
		$to = 'test@hotmail.com'; 
		$subjekt = 'Info';
		$msg= 'User with name $ime answered on question . His answer is $answer ';
		$headers = 'From: test@ba.com'; 
		mail($to,$subjekt,$msg,$headers);
}else{}

?>



I tried with this php code to figure out is button 1 or 2 clicked
if (isset($_REQUEST['b1'])){
  $answer='Opcija 1';
} else (isset($_REQUEST['b2'])) {
  $answer='Opcija 2';
}


Is This A Good Question/Topic? 0
  • +

Replies To: How to know which button was clicked.

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3577
  • View blog
  • Posts: 10,444
  • Joined: 08-June 10

Re: How to know which button was clicked.

Posted 25 June 2014 - 07:26 AM

the problem with your code is that only the clicked button’s name is sent, if at all. so $_POST['Send'] will never exist.

IMO the best option with submit buttons is making a form for each button and using hidden fields.
<form action="…">
  <input type="hidden" name="question" value="1">
  <input type="hidden" name="answer" value="1">
  <button type="submit">Answer 1</button>
</form>
<form action="…">
  <input type="hidden" name="question" value="1">
  <input type="hidden" name="answer" value="2">
  <button type="submit">Answer 2</button>
</form>
<!-- etc. -->





PS. Answer, not Ansswer.
Was This Post Helpful? 0
  • +
  • -

#3 zippp  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 76
  • Joined: 22-June 09

Re: How to know which button was clicked.

Posted 26 June 2014 - 10:57 PM

Hi, you can use the same form for multiple form actions with a bit of javascript code.

Method 1

<form id="myform" action="" method="post">
   <input type="submit" onclick="submitForm('result_b1.php')" name="b1" value="1st Ansswer"/>
   <input type="submit" onclick="submitForm('result_b2.php')" name="b2" value="2nd Ansswer"/>
   <input type="submit" onclick="submitForm('result_b3.php')" name="b3" value="3rd Ansswer"/>
   <input type="submit" onclick="submitForm('result_b4.php')" name="b4" value="4th Ansswer"/>
   <input type="submit" onclick="submitForm('result_b5.php')" name="b5" value="5th Ansswer"/>
</form>



So the form will change its action depending on which button you click. To do that, you need also this javascript:

function submitForm(action) {
	document.getElementById('myform').action = action;
	document.getElementById('myform').submit();
}



Now you need just to create 5 php files for each buton to display the results.

Method 2

Use the same php file to display the results.

<form id="myform" action="" method="post">
   <input type="submit" onclick="submitForm('result.php?res=b1')" name="b1" value="1st Ansswer"/>
   <input type="submit" onclick="submitForm('result.php?res=b2')" name="b2" value="2nd Ansswer"/>
   <input type="submit" onclick="submitForm('result.php?res=b3')" name="b3" value="3rd Ansswer"/>
   <input type="submit" onclick="submitForm('result.php?res=b4')" name="b4" value="4th Ansswer"/>
   <input type="submit" onclick="submitForm('result.php?res=b5')" name="b5" value="5th Ansswer"/>
</form>



Now in your result.php file you will have to check for the "res" parameter.

if(isset($_GET['res']) {
   if($_GET['res'] == 'b1' {
      echo 'B1 was clicked';
   } elseif ($_get['res'] == 'b2]' {
      echo 'B2 was clicked';
   }
   // and so on. you can also use SWITCH instead of if else condition
}



Method 3

Using checkboxes or radioboxes is way more simple.
Instead of 5 buttons, use 5 checkboxes. In the result page you just have to check if they are set and that's all.

This post has been edited by zippp: 26 June 2014 - 11:01 PM

Was This Post Helpful? 0
  • +
  • -

#4 Blindman67  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 134
  • View blog
  • Posts: 592
  • Joined: 15-March 14

Re: How to know which button was clicked.

Posted 28 June 2014 - 01:59 AM

Dormilich is on the right track. The best way to handle this is to create a hidden field and remove the form submit names from the buttons so each button is not sent on the post or get. Wrap one click event around all the buttons and then use the event argument to get which button is clicedk. Add the value to the hidden field and then submit the form.

<body>
<!-- create the form and set the action to your server script -->
<form action="yourServerScript.asp" id="myForm">
<!-- give the fieldset an id so you can set the click event -->
<fieldset id="answerButtons"> <legend> Question1 </legend>
<!-- add the buttons, Dont include the name attribute as you are not interested in getting each buttons info -->
<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"/>
<!-- the hidden feild that will hold the users selection  defaults to unanswered-->
<input type="hidden" name="answer" id="answerInput" value="unanswered">
</fieldset>
<!-- skip button with no name as you are not interested in what this contains -->
<input type="submit" value="Skip" >	
</form>
 

<script>
// this script can go anywhere on the page you want. Best is the head. 
// If you do not wish to use the addEventListener function you will have to put the script after the form 
// in the body of the page and assign the event directly to the element
// If you want to use IE6 or earlier this will not work. Lookup alternative to addEventListener for IE

// Set up the buttonclick event for the user input when the page loads
window.addEventListener("load",function(){
        document.getElementById("answerButtons").addEventListener("click",buttonclicked,false);
    },false);
    
    
// this handles the users input. e is the event object and is used to get info about the event
function buttonclicked(e){
    // e.target is the element that triggered the event
    // get the answer input and add the value of the events target
    document.getElementById("answerInput").value = e.target.value;
    // now submit the form.
    document.getElementById("myForm").submit();
    // the request will look like
    // yourServerScript.asp?answer=4th+Ansswer
    // or 
    // yourServerScript.asp?answer=unanswered
    // if the user click skip
}

</script>
</body>


It may pay to change the values of the answer values to 1,2,.. 5 to make it simpler for your server script. It is best to use element ID's to reference elements in a form rather than via their names. Remember do not name input you do not wish to send to the server.

Hope this helps.

This post has been edited by Blindman67: 28 June 2014 - 01:59 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1