简体   繁体   English

PHP检查链接是否被点击

[英]php check if link clicked

The question is that how do i check if a link has been clicked? 问题是如何检查链接是否已单击?

<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");
?>

The first a tag add a car to the cart and the second a tag add a boat to the cart. 第一个标签将汽车添加到购物车,第二个标签将船添加到购物车。 How do i detect which one of the a tag that has been clicked, and if i now click on any of the a tags both a car and a boat will be added to the cart. 我如何检测单击了哪个标签,如果现在单击任何一个标签,则汽车和船都将添加到购物车中。

You can add GET parameters to the links: 您可以将GET参数添加到链接中:

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

And then in your PHP document: 然后在您的PHP文档中:

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

This is basically the easiest way to pass data from one page to another using a link. 基本上,这是使用链接将数据从一页传递到另一页的最简单方法。

The concept that you should use. 您应该使用的概念。 is Ajax. 是Ajax。

Every click and others things with the browser only happens on the client ( browser ). 浏览器的每次单击和其他操作仅发生在客户端(browser)上。

  1. Then when you do click. 然后,当您单击时。 The browser has send request to server. 浏览器已将请求发送到服务器。
  2. The server, get values that you send from browser and it proccess. 服务器,获取您从浏览器发送并处理的值。

Some simple may be: 一些简单的可能是:

// Html // HTML

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

//Javascript // 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 file for first link //第一个链接的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, 

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM