View Full Version : Execute function only if I click link

09-13-2009, 03:40 PM
I have a standard php function which for example sends some data to database, but I don't want to execute this function every page load but only when I click a link on this page. How can I do that? How can I tell the function that the link is clicked?

Can anyone please help?

09-13-2009, 06:33 PM
two most common methods of this is by using POST or GET PHP predefined variables (http://us2.php.net/manual/en/reserved.variables.php).

GET is usualy used to comunicate with PHP with links, while POST is used to communicate with forms. Although you can also use GET with forms but that is not recommended.

Try this test.php to figure out how php works

if (isset($_GET['link'])) {

if(isset($_POST['form'])) {

function getFunction($variable) {
print $variable;

function postFunction($variable) {
print $variable;


<a href="?link=test_link">Test link for get</a>
<form action="" method="post">
<input type="submit" name="form" value="Test link for post" />