Or use AJAX. Depends on what you're trying to accomplish and how you're trying to accomplish it.
As far as the security goes, no matter what you do that part's going to be up to you, and I'd recommend not just blindly copy/pasting code from the internet in this situation.
We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.