Ray J Tong
Ray J Tong

Reputation: 897

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

Upvotes: 5

Views: 15494

Answers (4)

Jalal Ebrhaimi
Jalal Ebrhaimi

Reputation: 1

You can use another input tag after your submit button with hidden type.

<input class="ButtonSubmit" type="Submit" name="Submit" id="Submit" value="Submit"/>

    </p>
        <input type="hidden" name="submitted" id="submitted" value="true" />

after that use in top of your code this

if (isset($_POST['submitted'])) {
// your code is here
}

it's work for me. you can use it in wordpress template as well

Upvotes: 0

fey
fey

Reputation: 1348

It's easy to do using jQuery:

<script>
$(function(){
    $('#your_button_dom_id').click(function(){
        $.ajax({
            url: 'your_php_script_url',
            type: 'POST', // GET or POST
            data: 'param1=value1&param2=value2', // will be in $_POST on PHP side
            success: function(data) { // data is the response from your php script
                // This function is called if your AJAX query was successful
                alert("Response is: " + data);
            },
            error: function() {
                // This callback is called if your AJAX query has failed
                alert("Error!");
            }
        });
    });
});
</script>

You can read more about AJAX in jQuery here: http://api.jquery.com/jQuery.ajax/

Upvotes: 0

Rubin Porwal
Rubin Porwal

Reputation: 3845

You need to insert a record into mysql table upon click of button without reloading the page. For accomplishing the above task you need to use AJAX which will send http request to server in background using xmlhttprequest object and thereby updating web page without reloading the web page. So you will have to create a function in javascript which will send http request to server using xmlhttprequest object and also you need to define server side handler for processing http request sent using ajax. For implementation details of ajax with php ,please refer the example mentioned in below link http://www.w3schools.com/php/php_ajax_php.asp

Upvotes: 0

Telgin
Telgin

Reputation: 1604

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

Javascript

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.

Upvotes: 9

Related Questions