Page 1 of 1

javascript - php problem

Posted: Fri Sep 23, 2005 10:50 am
by hame22
Hi

I have designed an application that allows a user to download a pdf. When the user clicks the download link a javascript function runs that opens a new window for this download and will then redirect the first page to another page in the site.

However I am having the problem of the javascript redirect does not successfully open the intended download. If I use a basic link then this works fine however when javascript is introduced errors happen.

Below is my code I would be grateful if anyone has any ideas as to the problem, thanks

Code: Select all

<Script Language="JavaScript">

function load() {
window.open('download.php?act_id=<?php print $act_id;?>');
}
// -->
</Script>

the fnction that runs the download:

Code: Select all

function download_file($product_code)
{
	$row = activity_query($product_code);
	$location = $row['location'];
	
	
	$path ='../../../../fenman_learning_resource_products_05/'.$location.'';
	if (file_exists($path)) 
	{
		
		
		$size = filesize($path);
		$file = basename($path);
		
		header ("Content-Type: application/octet-stream");
		header ("Content-Type: application/download");
		header("Content-Disposition: attachment; filename=$file");
		header("Content-Length: $size");
		readfile($path);
		
		//update user's credits
		deduct_credits($product_code);
		
		//send confirmation email
		confirm_email($product_code);

	} 
	else
	{
		header("Location: $path");

	}
}

thanks

alex

Posted: Fri Sep 23, 2005 11:58 am
by feyd
was it worth creating yet another new thread about this?

Posted: Mon Sep 26, 2005 3:47 am
by hame22
if it helped in solving the problem then yes, isnt that what these forums are here for?

Posted: Mon Sep 26, 2005 4:03 am
by n00b Saibot
hame22 wrote:errors happen
can you explain this?

Posted: Mon Sep 26, 2005 4:22 am
by hame22
yes, basically what happens is that the file to be downloaded can not be found.

This only happens when it is opened via a javascript link, when opened via a form button the file is downloaded

Posted: Mon Sep 26, 2005 7:43 am
by shiznatix
then there is somthing wrong with your javascript. your product code must be wrong in the js