Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

monstajamss's avatar

How to pass javascript value to php variable

So i have a button that when clicked it displays the id like below

<?php
$id = 2;
?>
<buttton onclick="getId($id)">click me</button>
<script>
function getId(id){
 console.log(id) //displays 2
}
</script>

how do i pass the getId value to another php variable??

0 likes
1 reply
tykus's avatar

PHP has already been executed and "returned" by the time your Javascript can get to work; you would need a new Request to pass a value into PHP.

1 like

Please or to participate in this conversation.