Jonathan
Jonathan

Reputation: 49

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.

Upvotes: 0

Views: 9410

Answers (2)

ray
ray

Reputation: 171

The concept that you should use. is Ajax.

Every click and others things with the browser only happens on the client ( 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

<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 file for first link

<?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, 

?>

Upvotes: -1

woubuc
woubuc

Reputation: 927

You can add GET parameters to the links:

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

And then in your PHP document:

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.

Upvotes: 3

Related Questions