dongzi3805 2015-09-04 18:17
浏览 28
已采纳

Ajax访问链接而没有实际访问链接

I've got a basic like button concept on my site that visits url.tld?action=love and adds +1 to the link's database column.

It's a hassle redirecting to another page all the time though. Is it possible to click the button, and send a request to the URL without actually redirecting to a new URL? Also maybe refresh the button afterwards only so that the count updates?

For a general idea of what my download button is this is in the header:

<?php require_once('phpcount.php'); ?>
<p class="hidden"><?php
   $time = time();
   for($i = 0; $i < 1; $i++)
   {
     PHPCount::AddHit("$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]", "127.0.0.1");
   }
   echo (time() - $time);
   /*echo "PAGE1 NON: " . PHPCount::GetHits("page1") . "
PAGE1 UNIQUE: " . PHPCount::GetHits("page1", true);
   echo "

" . PHPCount::GetHits("page2");
   $ntot = PHPCount::GetTotalHits();
   $utot = PHPcount::GetTotalHits(true);
   echo "###$ntot!!!!$utot";*/?></p>

And this is an example of my "love" button.

<a href="https://alt.epicmc.us/download.php?link='.strip_tags($package_get).'?action=love" target="_blank" class="red-button">Love <span class="count">'. PHPCount::GetHits("$package_get?action=love", true).'</span></a>

The reason I used this method is because people create pages, and I wanted the like button to work out of the box. When their page is first visited it adds their url to the database, and begins tallying unique hits.

This is basically adding a new link column called downloadlink?action=love, and tallying unique clicks.

  • 写回答

2条回答 默认 最新

  • dtds8802 2015-09-04 18:32
    关注

    use the following code. assgin id="btn_my_love" to that button and add this code to you page

     <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
    
    <script>
          //assign url to a variable
          var my_url = <?php echo "https://alt.epicmc.us/download.php?link='.strip_tags($package_get).'?action=love"; ?>;
    
          $(function(){
    
           $("#btn_my_love").click(function(){
              $.ajax({
                url:my_url,
                type:'GET',
                success:function(data){
                   //comment the following result after testing
                   alert("Page visited");
                },
                error: function (request, status, error) {
                   alert(request.responseText);
                }
              });
              //prevent button default action that is redirecting
              return false;
            });
    
          });
    </script>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?