2014-12-13 143 views
0

问题是如何检查链接是否被点击?php检查链接是否被点击

<a href="laggtill.php">+</a> 
<a href="laggtill.php">+</a> 

(another document) 
<?php 
session_start(); 

$_SESSION['car'] = $_SESSION['car'] + 1; 
$_SESSION['boat'] = $_SESSION['boat'] + 1; 

header("Location: betalning.php"); 
?> 

第一个标签添加一辆车到购物车,第二个标签添加一条船到购物车。我如何检测哪个标签被点击过,如果我现在点击任何一个标签,车和船都将被添加到购物车中。

+0

你想要通知页面还是服务器? – jasonszhao 2014-12-13 01:15:11

+0

http://api.jquery.com/click/这是你在找什么? – jasonszhao 2014-12-13 01:17:30

回答

1

您可以添加GET参数的链接:

<a href="laggtill.php?add=car">Add car</a> 

然后你的PHP文件中:

if($_GET['add'] == 'car'){ 
    $_SESSION['car'] += 1; 
} 
// etc... 

这基本上是用从一个页面的数据传递到另一个的最简单方法链接。

+0

谢谢!它的工作完美:) – Jonathan 2014-12-13 01:31:11

-1

您应该使用的概念。是Ajax。

浏览器的每次点击和其他事情只发生在客户端(浏览器)上。

  1. 然后当你点击。浏览器向服务器发送请求。
  2. 服务器,获取您从浏览器发送的值并进行处理。

一些简单的可能是:

//的Html

<a id="linkone" href="laggtill.php">+</a> 
<a id="linktwo" href="laggtill.php">+</a> 

//使用Javascript

// Use jquery 
$("#linkone").on("click", function(){ 

    //function that send request to server 
    $.post('someurl.php', {}) 
     .success(function(res){ 
      console.log(res); 
      // If you stay here, the procces should be ended 
     }) 
    // if you return false, the click dont redirect other window 
    // if you return true, the click redirect other window 
    return false; 
}); 

// PHP的第一个链接文件

<?php 
    //capture values 
    // But only is a clic, then there is not values 
    session_start(); 

    $_SESSION['car'] = $_SESSION['car'] + 1; 
    // If you want some simple, one file only works for one link 
    // For the other link, you should create other file same to this. 
    header("Location: betalning.php"); // With ajax dont use this line, 

?>