Ray J Tong Ray J Tong - 8 months ago 19
PHP Question

Button that runs a php script without changing current page

I have a webpage that generates a table from mysql. I have a button at the beginning of each row. I would like it so if the user decides to press on the button, the contents of that individual row are written to a new table in MySQL.

Currently I am thinking of just having the button be an href to another php script that connects to mysql and inserts the row into a table; however, I think that will redirect my current page.

I would like the button to run the script, without redirecting my current page. That way, the user can continue analyzing the table without having the page have to reload every time.

This is what my current table looks like. This is just a snippet, and the table can be very large (hundreds of rows)
enter image description here


In order to do this client side, there are a couple of ways I can think of off hand to do this:


You can include a Javascript library (like the ever popular JQuery library), or code it yourself, but you could implement this as an XMLHTTPRequest, issued from a click handler on the button. Using a library is going to be the easiest way.

An iframe

Create a hidden iframe:

<iframe style="display:none;" name="target"></iframe>

Then just set the target of your tag to be the iframe:

<a href="your_script.php" target="target">...</a>

Whenever someone clicks on the link, the page will be loaded in the hidden iframe. The user won't see a thing change, but your PHP script will be processed.

Of the two options, I'd recommend the Javascript library unless you can't do that for some reason.