duanhuantong8278 2012-12-06 11:27
浏览 58
已采纳

Ajax调用将click事件放入php变量以避免window.open()方法

I have this jQuery scrip which counts the number of clicks on a certain div and then, at the second click, it redirects the user to a custom url. The script works and counts the clicks even if the user leaves the page.

function traffic(){
    var traffic = $('.model').data("traffic");
    if(traffic){
        var nbc = localStorage['nbc']||0;
        $('.model a').click(function(){
            localStorage['nbc'] = ++nbc;
            if(nbc%2==0){
                window.open(traffic, '_blank');
            }
        });
    }
} 

Now, I am a total newb with ajax (actually i do not know anything about it, I am a webmaster not a programer with an overdue deadline) and I need some help:

I need an ajax call that, on the time of the action, will send the second click into a php variable so I can use a code like the following (and avoid the "window.open" method which is in conflict with another script I use on the page):

   if($number_of_clicks == 2){
       echo 'my new link, a method which will save me from using window.open';
   } else {
       echo 'my ragular link';
   }

Can it be done?

UPDATE

Let me explain, my fault for not being clearer.

User enters my site and clicks on a normal link. Jquery stores that event and the user goes to an internal page.

When he hits the home button, ajax sends that event to a php variable, so I'll have $myvar=1.

Now I can change that certain link on my site with:

if($myvar == 1){
           echo 'my new link';
       } else {
           echo 'my ragular link';
       }

And now, when the user clicks on that link, he will go to the new link.

Hope I've been a little clearer.

Ty!

  • 写回答

2条回答 默认 最新

  • doujia1679 2012-12-06 11:43
    关注

    The only way to get a click to PHP is to just an AJAX request. EG:

    $.get('/clickonce.php');
    

    and then in PHP you could do:

    <?php
    $myvar = 1;
    ?>
    

    But this wouldnt be perfect. The clickonce.php code will be called, but there is a delay since it's a new page request, so with slow internet/server responds it could be more then a few seconds before it is actually logged.

    Considering that your window.open is conflicting with another script, which I assume is javascript, i would go for a javascript solution.

    var clickedOnce = false;
    function traffic(){
        var traffic = $('.model').data("traffic");
        if(traffic){
            var nbc = localStorage['nbc']||0;
            $('.model a').click(function(){
                localStorage['nbc'] = ++nbc;
                if(nbc%2==0){
                    window.open(traffic, '_blank');
                } else {
                    clickedOnce = true;
                }
            });
        }
    } 
    

    now in your other javascript script you can check for if (!clickedOnce) { //do something }

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)