-2

I want to change the href of a link after it is clicked 10 times. Every time someone clicks the link a number in a database increments by 1.

then i see if the row is less than 10

include'connect.php';

$result = mysqli_query($con,"SELECT id, link_name, click  FROM clicks WHERE click< 15");

while($row = mysqli_fetch_array($result))
{
$clicks = $row['click'];
echo $clicks;
}

I just put the echo to see if it was working i need to replace that echo with jquery to change the href of a link to the new one. Is that smart or bad coding trying to integrate two languages together? since it cant be done through jquery or php individually.

Rodrigo Lessa
  • 69
  • 3
  • 11
  • 11
    You need to understand the difference between server-side code and client-side code. – SLaks Aug 28 '13 at 19:43
  • this process cant be done with jquery alone or php alone – Rodrigo Lessa Aug 28 '13 at 19:44
  • 3
    Then you need to learn about AJAX. – SLaks Aug 28 '13 at 19:44
  • It could work with only php but he would need to create the link every time if the count is smaller then 10. – Sebastien Aug 28 '13 at 19:47
  • PHP can output text, the browser can interpret text as javascript. javascript cannot execute php. javascript can however send an http request to the webserver which then calls a php file that executes php script. – Kevin B Aug 28 '13 at 19:48
  • If these pages are in the same server, you can check the `$_SERVER['HTTP_REFERER']`, and if it's coming from the other page you can then save it in the database as a click, that way you avoid using jQuery altogether. I would suggest you learn to do AJAX calls, it'll come in handy for the future. – Hanlet Escaño Aug 28 '13 at 19:49
  • Further to SLaks comment, [here are some examples](http://stackoverflow.com/questions/17973386/ajax-request-callback-using-jquery/17974843#17974843) to get you started with AJAX. – cssyphus Aug 28 '13 at 19:54
  • @RodrigoLessa If any of those examples were/are helpful to you, please upvote them. – cssyphus Aug 28 '13 at 20:39

2 Answers2

1

presumably you want to use Jquery because you want to change the href without reloading the page? save your php as noOfClicks.php amd add the javascript to your main target page

$.get('noOfClicks.php',function(result){
if (result>10){
 $('#yourAnchor').attr('href',"http://www.newlink.com");
}
});
andrew
  • 9,313
  • 7
  • 30
  • 61
  • this doesnt work for the total of times clicked just since its last reload i need it to save all users – Rodrigo Lessa Aug 28 '13 at 19:53
  • this gets the number of times clicked from the data base using your php using $.get. which is a simple Jquery Ajax call http://api.jquery.com/jQuery.get/ – andrew Aug 28 '13 at 19:59
  • so i just link it to my php function where it selects the row im checking for? – Rodrigo Lessa Aug 28 '13 at 20:05
  • save the php code you posted in a php file for example noOfClicks.php, place the java script I posted in your main page eg index.php, replace yourAnchor with the id if your link and newlink.com to your new location. When a user clicks the link, it will query the database (via AJAX, via noOfClicks.php) if the response is > than 10 the href will be changed – andrew Aug 28 '13 at 20:10
0

You could do it just with php:

<div id="WhereYourLinkisContain">
    <?php 
    include'connect.php';

    $result = mysqli_query($con,"SELECT id, link_name, click  FROM clicks WHERE click< 15");

    while($row = mysqli_fetch_array($result))
    {
        echo "<script>function(){ $('#IdOfLinkHere').attr('href', ".$row['link_name'].")}</script>";
    }
    ?>
</div>

EDIT see echo

Sebastien
  • 1,308
  • 2
  • 15
  • 39