javascript - php problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hame22
Forum Contributor
Posts: 214
Joined: Wed May 11, 2005 5:50 am

javascript - php problem

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

was it worth creating yet another new thread about this?
hame22
Forum Contributor
Posts: 214
Joined: Wed May 11, 2005 5:50 am

Post by hame22 »

if it helped in solving the problem then yes, isnt that what these forums are here for?
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

hame22 wrote:errors happen
can you explain this?
hame22
Forum Contributor
Posts: 214
Joined: Wed May 11, 2005 5:50 am

Post 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
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

then there is somthing wrong with your javascript. your product code must be wrong in the js
Post Reply