Click here to Skip to main content
15,900,815 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Actually, I want to get the inner text of clicked button store it in a variable and pass the value to a php variable. Everything is fine but the problem comes during passing the javascript value to php variable. If I assign a custom value to the variable then the value will be passed but the value of the inner text of the button is not passing.
Here is my code:

What I have tried:

HTML
<html>
<button id="btn">esteem</button>

<button id="btn">aqas</button>

<button id="btn">umar</button>

</html>



<script>
var buttons = document.querySelectorAll("[id='btn']");
var buttontext;
for(var i=0; i<buttons.length; i++){
    buttons[i].addEventListener("click", function(){buttontext=this.innerHTML;
   // alert(buttontext);
    console.log(buttontext)
   }
  )
}
<?php 
             $ff ="<script>document.write(buttontext);</script>";             
          ?>
</script>
Posted
Updated 18-Dec-19 23:19pm

1 solution

Is tough to guess what exactly your application is doing by looking at a little bit of code. I would recommend the following just based on what posted here.

1. Add a hidden field
HTML
<input type="hidden" id="btnClickedValue" name="btnClickedValue" value="" />


2. Store the button inner text into a hidden field each time the button clicked
JavaScript
document.getElementById("btnClickedValue").value = buttontext;


3. Then, on the backend / php side, you can write something like below to get the button innerText
PHP
echo $_POST['btnClickedValue'];


Again, this is just based on what posted here.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900