view raw
Benjamin Oats Benjamin Oats - 9 months ago 36
jQuery Question

inject last part of URL dynamically and uniquely using javascript or php

I am trying to work out how to get thye last part of a url on click of a button dynamically from a pre populated csv file.

For example on click of

<a href="" >button</a>

would grab a code from a CSV file (or something better) and the link would be

However each code from the CSV will only be used once and be removed after

So first click =

Second click =

and so on getting
from the CSV file and never to be gotten again.

I can use either JavaScript or PHP, either is good, I'm just not sure on the syntax.


Like this:

 $first = true;
 $links = "";
 $file = fopen('myCSVFile.csv', 'r');
 while (($line = fgetcsv($file)) !== FALSE) {
   if (!$first) $links.=",";
   //$line is an array of the csv elements

Plain JS

var links = [<?php echo $links ?>];
cnt = 0;
window.onload=function() {
  document.getElementById("csvLink").onclick=function() {[cnt],"_blank"); // otherwise you need cookies
    return false;

jQuery using Ajax:

var links = [<?php echo $links ?>];
cnt = 0;
$(function() {
  $("#csvLink").on("click",function(e) {