Is there a way to send data to database when click on a link without page refresh?
I use php/mysql...
Is there a way to send data to database when click on a link without page refresh?
I use php/mysql...
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.
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/
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/ ).
Yes, you can use AJAX.
This is a very big topic, but I'd recommend you do some research on AJAX and jquery (javascript).
Here are some tutorials:
http://www.ajaxf1.com/tutorial/ajax-php.html
http://www.talkphp.com/vbarticles.php?do=article&articleid=58&title=simple-ajax-with-jquery
Do a search in google for more info.
At first Sorry for my English or if i couldn't explain very well. So 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 jQueyr 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.