This is a common question amongst beginner developers. And I still remember how this question crossed my mind back when I first started out writing PHP scripts.
However, there is a way to do what you’re trying to do.
So let me explain, because if you are searching for this question, it means that you don’t fully understand what is going on.
When the user navigates to your PHP script, this is what happens:
- Your web server receives the request from the browser and it loads the relevant PHP file.
- Your PHP code is parsed and executed.
- The web server gathers the response / output and sends it back to the browser.
- The browser displays this output.
It’s over, mate. Move on.
OK. So what can I do?
To access a PHP script without reloading the page, you will need to use Ajax. Using Ajax, you can open a new connection to the PHP on your web server without forcing the user to navigate away from the page. And the best thing is: It all happens in the background, so the user is blissfully unaware and left undisturbed.
By using Ajax, you can access PHP scripts and return a response to the browser in the background. You can then use this response to dynamically change content that is on the page.
If you think that Ajax will solve your problem, then you should read some of the tutorials that I have written on the subject:
Hopefully, this article saved you time by clearing up any confusion and sending you down the right path.