Skip to content Skip to sidebar Skip to footer

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...

Solution 1:

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 ).

    $("#button_id").click(function(){
    var var_data = 5;
    $.ajax({
            url: "my_script.php",
            data: { var_PHP_data: var_data };
            success: function(data) {
                // do something;alert(data);
            },
     });
});

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.

<?php$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.

Solution 2:

You'd have to use JavaScript. When a user clicks a link, if you don't use JavaScript, then you need to go user -> server -> user and they get a whole new page.

HTTP is stateless.

Solution 3:

It's not possible without a page refresh but this is the classic use-case for AJAX requests. If you're not familiar with AJAX then there are various methods of doing this using all the popular JavaScript frameworks such as Prototype and jQuery

Solution 4:

You can't send data directly to a database, but you can use AJAX to send data to a php page that will save them to the database, without reloading the page or following the link in the browser..

Have a look at http://api.jquery.com/jQuery.post/

Solution 5:

Not using PHP because it is server side - you need JavaScript / AJAX for this.

Check out frameworks like dojo (http://www.dojotoolkit.org/) , mootools (http://mootools.net/) or jQuery ( http://jquery.com/ ).

Post a Comment for "Send Data To Database When Click On A Link Without Page Refresh"