Home > Net >  Set PHP varibles on button press without using POST or GET
Set PHP varibles on button press without using POST or GET

Time:07-30

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.

CodePudding user response:

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.

CodePudding user response:

If you work with PHP JS, and refreshing the whole page is not an option, what you need is an AJAX call that will fire on your onClick().

AJAX refreshes just a part of your page.

It will send data to your PHP file, you can re-set your variables there as you need, and then there will be a response. This response you can then parse in your JS.

Basic structure should be like this:

onClick() -> .ajax() -> PHP script -> response -> process response in JS
  • Related