Click here to Skip to main content
14,644,614 members
Rate this:
Please Sign up or sign in to 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>
<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

Rate this:
Please Sign up or sign in to vote.

Solution 1

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
<input type="hidden" id="btnClickedValue" name="btnClickedValue" value="" />


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


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


Again, this is just based on what posted here.
   

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100