Francky
Francky

Reputation: 67

Button which execute code once clicked then reload same page to confirm

Sorry for my english. I'm learning php and I create a small member area with very basic functions.

I try to create a simple "Activate" button which, when pressed, update mysql row (0 to 1). But I don't understand how I can do that without the need to call a form and an other php page (then redirected to my original page with this time a message saying "button was pressed" instead of the original button). When the button is clicked, I would like it reloads the same page, then exec the code to update mysql, and not call an other... And I don't want to update mysql just by refreshing the page (button must be clicked).

I know how to update mysql and play between the display of the button/message, my problem is just about the execution and refresh on the same page once button is clicked. Thanks!

Upvotes: 0

Views: 342

Answers (2)

Andrew
Andrew

Reputation: 20091

You could just add a GET parameter to your button's url.

<a href='http://example.com?activate=1' class='btn'>activate</a>

And in your php access it

if (isset($_GET['activate'])){
    //do your query stuff

   header("Location: http://example.com");
   die;
}

Upvotes: 0

jirig
jirig

Reputation: 551

Your form can be sent to the same page (in html 5 simply by omitting the action attribute of form). Your php script should handle two scenarios -

  • a standard request where you render your page with button
  • a form reponse (either via post or get) where you perform your database query, print a success message saying "button was pressed" and maybe the form with the button disabled (in case you want that).

Upvotes: 1

Related Questions