I am trying to change the value of a php variable when the user clicks a html li element which has been generated using php. I am not getting an error but it doesn’t appear that the variable is changing. Is there something wrong with the below code? Note it is inside a php file within php tags. Thanks.
$testvalue = "a";
echo '<li onClick="'.$testvalue.' = \''."b".'\' ">test</li>';
Edit:
I fixed it! Super happy. I was able to use javascript to parse the variable where I needed it before the php function was called. Thanks for the help guys…or girls (probably not 🙂 )
You cannot do this.
PHP code is executed server-side. Your
onclickfunction is executed client-side. In other words, by the time the user clicks on this<li>element, the PHP code has completed execution.You can, however, try using AJAX to make your JavaScript interact with your server, depending on what you are actually trying to accomplish.