Onclick point to function with $_SERVER

Asked

Viewed 47 times

1

Due to an obscure need of the system, when clicking a button, I need to know the IP that triggered it to direct the correct page. With $_SERVER['REMOTE_ADDR'] I have this information. But how could I pass it to a JS ? More or less this way:

document.getElementById('LIMPAR').onclick = function() {
      if ($_SERVER['REMOTE_ADDR'] == 'XXX') {
         location.href = '1';
      } ELSE {
         location.href = '2';
      }
} 

Testing the above logic, I was unsuccessful. Any suggestions?

  • Maybe in the onclick attribute of the wipe button, call a function to do this redirection, passing as parameter the $_SERVER['REMOTE_ADDR'] coming straight from PHP when mounting the HTML.

1 answer

1


You are trying to access a context variable from PHP, server, on client side, javascript.

Try something like that:

document.getElementById('LIMPAR').onclick = function() {
  var ip = '<?php echo $_SERVER['REMOTE_ADDR']; ?>';
  if (ip == 'XXX') {
    location.href = '1';
  } ELSE {
    location.href = '2';
  }
}
  • Exactly, that way right. Thank you.

Browser other questions tagged

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