0

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

Victor Bocharsky
  • 11,930
  • 13
  • 58
  • 91
wcp
  • 27
  • 1
  • 1
  • 7

3 Answers3

2

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

Patrick Evans
  • 41,991
  • 6
  • 74
  • 87
  • i think your solution is more of what i was looking for, because my query already carries a post action, thanks, ill try it and get back – wcp Aug 13 '14 at 12:06
0

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.

elpaulo
  • 86
  • 4
0

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.
 }
Raphael Müller
  • 2,180
  • 2
  • 15
  • 20
  • can u please apply that to the snippet i gave – wcp Aug 13 '14 at 11:44
  • i don't know your link, and i don't know the variable you want to send. but i can write some suggestions. – Raphael Müller Aug 13 '14 at 11:46
  • Don't use GET-parameters for update actions in the database. He should use a POST-action in his case. – Thijs Aug 13 '14 at 11:54
  • 1
    @Thijs how do you send a POST header with http in a link? – Raphael Müller Aug 13 '14 at 11:55
  • @RaphaelMüller He should use AJAX. – Thijs Aug 13 '14 at 11:57
  • @Thijs so you say if you use some rewrite rules on your server like: `example.com/api/save` and pass the `save` as action variable (`$_GET['action']`) to your php script, you can't do that, you have to use ajax and post? – Raphael Müller Aug 13 '14 at 11:59
  • this is my link Home – wcp Aug 13 '14 at 12:00
  • @RaphaelMüller, I think every action that change something to the database should be using a POST-request. (http://stackoverflow.com/questions/1254132/so-why-should-we-use-post-instead-of-get-for-posting-data). If he doesn't want to use AJAX, he could use a form. – Thijs Aug 13 '14 at 12:04
  • @Thijs I agree with you if you have a REST api or somthing similar, then you have to use GET, POST, DELETE, PUT, MERGE etc. But if you only want to pass some value (no formdata) while clicking a link, you can only use GET without the use of javascript. With this way it's also safe for search machines, of course it's not SEO friendly. – Raphael Müller Aug 13 '14 at 12:09
  • @RaphaelMüller Forget the JavaScript and AJAX, why not using a form? – Thijs Aug 13 '14 at 12:14
  • @Thijs because the question of this thread was to use a link ;) – Raphael Müller Aug 13 '14 at 12:18