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

5
  • First of all, format your post, it's hard to read. What have you tried and what did not work? Commented Aug 13, 2014 at 11:32
  • If you are wanting to stay on the same page you will need to look into using ajax Commented Aug 13, 2014 at 11:33
  • Without reloading the page ? use AJAX ! Commented Aug 13, 2014 at 11:33
  • 1
    Send parameters via _GET Commented Aug 13, 2014 at 11:33
  • i dont intend to stay on the page, i just want to make sure that onlick of that link, the query be excuted, Commented Aug 13, 2014 at 11:37

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

1 Comment

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

Comments

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

11 Comments

can u please apply that to the snippet i gave
i don't know your link, and i don't know the variable you want to send. but i can write some suggestions.
Don't use GET-parameters for update actions in the database. He should use a POST-action in his case.
@Thijs how do you send a POST header with http in a link?
@RaphaelMüller He should use AJAX.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.