How to check if a link has been clicked? JS/PHP

Discussion in 'JavaScript' started by Make a perfect site, Jul 26, 2011.

  1. #1
    Hi all,

    I have a link in my php file here:

    piece of file1.php :
    <a href="file2.php">Add this to DB</a>
    HTML:

    This takes me to a page where the data gets inserted into my database.

    file2.php :

    
    <?php
    // data gets inserted
    
    header("Location: file1.php");
    ?>
    
    PHP:
    Then it gets redirected back to file1.php
    But when the user gets back to file1.php, he/she can click the link again. So he can insert the data a millions times into the DB. But I don't want that. I want to disable the link.
    I thought that JavaScript is the right language for this, but I don't know how. :(

    Is there any way to do this.

    Any help appreciated.

    Thanks in advance.
     
    Solved! View solution.
    Make a perfect site, Jul 26, 2011 IP
  2. Perpetual infinity

    Perpetual infinity Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Cookies can be used server-side to disable the link by either completely removing it or change its function.
     
    Perpetual infinity, Jul 27, 2011 IP
  3. #3
    PHP will do that for you not Javascript..

    you can use sessions and/or cookies..

    file1.php
    
    <?php 
    // this should be on the top of the page 
      session_start(); ?>
    .....
    <?php 
    // check if is_clicked exists
    if(!isset($_SESSION['is_clicked']) || !isset($_COOKIE['is_clicked'])){ ?>
    <a href="file2.php">Add this to DB</a>
    <?php } ?>
    
    PHP:
    file2.php
    
    <?php 
    // this should be on the top of the page 
      session_start(); 
    
    // do the data insert here...
    
    $_SESSION['is_clicked'] = 1;
    seetcookie('is_clicked', 1);
    header("Location: file1.php");
    ?>
    
    PHP:
    this is just a quick fix for your question..
    but if you use user login, you can save that value in the database because cookies and sessions do expire..
     
    JohnnySchultz, Jul 28, 2011 IP