Skip to content
Advertisement

Set PHP varibles on button press without using POST or GET

I need to set 3 PHP variables on a button press, but the problem is I can’t use POST or GET to do it due to it refreshing the page which breaks my JavaScript code as it includes an onClick event listener.

What I want my code to do exactly is, on a button press set 3 variables, $title, $description and $button_value. When the button is pressed it also sets off the JavaScript code to display a box with some information that is the 3 PHP variables.

Is there anyway to get around this issue or would I just have to rewrite my JavaScript code to work with POST/GET? I’m new to PHP and JavaScript so apologies for my incompetence.

Advertisement

Answer

If you want to run some PHP code while clicking the button and don’t want to refresh it, then you can send ajax request to your server using javascript. You can also update your page dynamically using the same.

Advertisement