Jump to content

AJAX add download


kaz3

Recommended Posts

On my site I am using AJAX so that when a user clicks a link, the file is downloaded and the number in the download number file(a txt file) is increased(later it will be a database). The problem is that sometimes it works in different browsers and it can never add to two files on the same page without refreshing.

 

Add download function:

function addDownload(name){
var xmlHttp=GetXmlHttpObject();
if (xmlHttp==null){
  		alert ("Your browser does not support AJAX!");
  	} 

var dlid=document.getElementById("downloads_"+name);
var dl=dlid.innerHTML;
dl=parseInt(dl);

dl+=1;
dlid.innerHTML=dl+" downloads";//Change the number on the page

var url="ajax/add_download.php";
url+="?name="+name;
url+="&sid="+Math.random();

xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}

 

PHP add download function:

<?php

$name=$_REQUEST['name'];
echo $name."<br>";

$file="../files/downloads/$name.txt";

$f=fopen($file,"r");
$downloads=fread($f,filesize($file));
fclose($f);

echo "old: ".$downloads."<br>";

$downloads++;

$f=open($file,"w");
fwrite($f,$downloads);
fclose($f);

echo "new: ".$downloads;
?>

 

The downloads do get written if I just go to the add_download.php page, so thats not the problem. Any ideas?

Link to comment
https://forums.phpfreaks.com/topic/109021-ajax-add-download/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.