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!
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 }
Well, you can simply make an AJAX get request using jQuery.
$.get('/your.php', function(url) {
// value echoed by your php page will be available in the url variable passed into this function.
// You can do what you want with it.
alert(url);
});