1

end goal: HTML element is click i want to run PHP functions

so i want to do something like this

  function byeBye(page) {
    if (page == 'teacher.php') {
      <?php $_SESSION['AccType'] = 1; ?>
    } else if (page == 'admin.php') {
      <?php $_SESSION['AccType'] = 2; ?>
    }
    console.log('Redirecting to ' + page);
    window.location.href = './' + page;
  }
</script>

and it called with something like this <li id='admin' class="list-group-item" onclick="byeBye('admin.php')"> but you can see what is wrong with that. what is the right way to do this?

Foxxything
  • 35
  • 1
  • 5
  • You need to understand that javascript is executed on the client side (in the browser), while php is executed on the server side. You cannot simply call a function, because those are completely separate systems. You need to make an ajax request from the client to the server. You will find many examples for that here on SO or on google. – arkascha Feb 06 '22 at 16:21
  • It seems, you are using PHP only to set the session, right? – Wahyu Kristianto Feb 06 '22 at 16:22
  • thats correct. thats all i need it to do – Foxxything Feb 06 '22 at 16:23
  • 1
    Some good reading: [What is the difference between client-side and server-side programming?](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) – Louys Patrice Bessette Feb 06 '22 at 16:24
  • i do get the difreance its just that I don't see any way to do what I want without js – Foxxything Feb 06 '22 at 16:24
  • 1
    As others have said, PHP and JS are separate systems. If your code is as simple as that, why don't you set a session on the destination page? – Wahyu Kristianto Feb 06 '22 at 16:26
  • because the next page will have something like [this](https://pastebin.com/6nhyQxtS) I only want the user if it has the session var – Foxxything Feb 06 '22 at 16:29
  • 1
    You should set that session value on login, depending on the user. Anything that could change the user type from the client side would be unsecure. – Louys Patrice Bessette Feb 06 '22 at 16:31

0 Answers0