King Julien King Julien - 1 year ago 62
SQL Question

Send data to database when click on a link without page refresh

Is there a way to send data to database when click on a link without page refresh?

I use php/mysql...

Answer Source

I will give you an example using jQuery.

Let's say that we have a link with an attribute id="button_id" (you have to learn the jQuery selectors ).

    var var_data = 5;
            url: "my_script.php",
            data: { var_PHP_data: var_data };
            success: function(data) {
                // do something;

Explanation: you will send the variable var_data with the name var_PHP_data to a my_script.php without page refresh using an ajax call (using GET method).

This is very simple example of what you have to write on your PHP script.

$var_name = $_GET['var_PHP_data'];
echo 'This is what you have send'.$var_name;


Because the default method to send variables in the ajax function in jQuery is GET.

We have to use the $_GET function in PHP.

This php script will print a message and this message will be handled in the success: function in the Ajax call and just for example we will alert this message returned from PHP.