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条)

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)