wcp
wcp

Reputation: 27

How To trigger a php query using a link

i have a page, in where by i can want to collect and store the amount of page views, i have a php query that stores my page views onclick of a button,

if(isset($_POST['submit'])){
    mysql_select_db($database_epl, $epl);
    $query_lin = sprintf("SELECT * FROM topic WHERE id = %s ORDER BY `Date` DESC", GetSQLValueString($colname_lin, "int"));
    $topicId = $_GET['id']; // you need to change 'id' to the name of your ID-parameter in the URL
    $viewsIncrementQuery = "UPDATE `topic` SET `Views` = `Views` + 1 WHERE `id` = " . $topicId;
    $incremented = mysql_query($viewsIncrementQuery, $epl) or die(mysql_error());
    // run/execute mysql query

but now i want to be able to call that query using links, (on click of a link)

How can i go about this

Upvotes: 0

Views: 1604

Answers (3)

Patrick Evans
Patrick Evans

Reputation: 42736

You can add a onclick event to the link and have it set a form's action attribute and then trigger a submit

html

<form method="post" id="myForm">
   <input type="text" name="id" />
   <input type="button" name="submit" value="submit">
</form>

<a href="/somePhpScript.php" onclick="submitForm(this)">Click to submit</a>

Javascript

function submitForm(element){
    var action = element.href;
    var form = document.getElementById("myForm");
    form.action = action;
    form.submit();
}

Your data would then be in the $_POST global array, if you want it in the $_GET global then just change the method attribute to get

Upvotes: 2

Raphael M&#252;ller
Raphael M&#252;ller

Reputation: 2200

Pass a parameter with your link:

http://example.com/yourpage.php?send=123

in yourpage.php

var_dump($_GET['send']);

returns

string(3) => 123

EDIT:

in your html output introduce a parameter for your link:

 <a href="http://example.com/yourpage.php?updateview=1">link</a>

in yourpage.php

 if($_GET['updateview'] == 1)
 {
      //your sql query etc. from the question.
 }

Upvotes: 0

elpaulo
elpaulo

Reputation: 96

You need to call this page with an ajax query, like this in JQuery. With this, you'll be able to transmit parameters as GET or POST, and receive them in your page, where you will do your update query.

Upvotes: 0

Related Questions