Run php within a javascript condition

Asked

Viewed 286 times

2

You can run php within a javascript condition?

Example:

<script>
if(variaveljs==2){
  <?php
  $variavelphp=20;
  ?>
}
</script>

  • What’s wrong with this code?

  • You can do whatever you want this way there. More every time you pass the value to Javascript you have to write the values with echo.

1 answer

2


PHP runs on the server side, and it always runs before the Javascript code, which runs on the client side. So that condition will not work, if you want to send client data to the server you have to use ajax

  • I had studied a little bit of jquery, which I had already used but never understood very well, and managed to solve the problem by putting the php code in another page and using the load function. Thank you very much :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.