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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.