I’m trying to do something with PHP when a text box changes. I can do this with JavaScript with onchange but that doesn’t work with PHP.
I already have a PHP function in my already existing PHP code. When a text box value changes, I want it to run the function.
2
Answers
The reason you can do it in JavaScript is because you’re in the same scope…the client side. PHP is a server-side language, so it has no notion of what is happening on the client-side, unless you explicitly tell it.
To tell PHP to evaluate, and possible return the response of a function call, you have to pass it the value of the input using a network call, such as a fetch or ajax request.
Your question shows that you really don’t understand PHP, and you need to learn the fundamentals of it. PHP does not run client-side, and JavaScript does not run server-side (again, unless you’re using Node, in which case you wouldn’t be using PHP).
Your PHP runs on your server-side. In your browser it is Javascript that runs. If you want to trigger PHP code to be executed upon some browser event, then you need to create a Javascript event-handler that will send a request to the server. Options:
But, before you start doing one of the above-mentioned possibilities, rethink what you actually need and whether your scenario really needs PHP code to be executed.