?
Solved

Problem in redirection after download in php

Posted on 2009-02-21
6
Medium Priority
?
490 Views
Last Modified: 2012-05-06
I am trying to pass the Query string from form1.php to formprint.php. in formprint.php i would like to take query string values and force the user to accept his details for download. After download popups up I would like to redirect to thanks page and also i would like to send an email to me with customer details. I have tried some code.  I am pasting here.

I have tried options like header location redirect, meta tag, echo script tag etc to redirect.

 Please look my code.
form1.php
 
<html>
<head> 
</head>
<body>
<a href="formprint.php?id=1">Profile 1</a> <br />
<a href="formprint.php?id=2">Profile 2</a> <br />
<a href="formprint.php?id=3">Profile 3</a> <br />
<a href="formprint.php?id=4">Profile 4</a> 
</body> 
</html>
 
 
formprint.php
 
<?php
 
 
$id=$_REQUEST['id'] ;
 
if ($id==1)
{
	
echo "<form action='abcd.php?id=1' method='post'>
Name: <input type='text' name='name' /><br />
E-Mail: <input type='text' name='email' /><br />
Phone: <input type='text' name='phone' /><br />
<input type='submit' type='submit' value='submit' />
</form>";
 
}
elseif ($id==2)
{
	echo "<form action='abcd.php?id=2' method='post'>
Name: <input type='text' name='name' /><br />
E-Mail: <input type='text' name='email' /><br />
Phone: <input type='text' name='phone' /><br />
<input type='submit' type='submit' value='submit' />
</form>";
	
}
 
elseif ($id==3)
 
{
	
	echo "<form action='abcd.php?id=3' method='post'>
Name: <input type='text' name='name' /><br />
E-Mail: <input type='text' name='email' /><br />
Phone: <input type='text' name='phone' /><br />
<input type='submit' type='submit' value='submit' />
</form>";
}
elseif ($id==4)
{
	echo "<form action='abcd.php?id=4' method='post'>
Name: <input type='text' name='name' /><br />
E-Mail: <input type='text' name='email' /><br />
Phone: <input type='text' name='phone' /><br />
<input type='submit' type='submit' value='submit' />
</form>";
}
 
else
{}
 
 
?>
 
 
abcd.php
 
 
<?php
 
//
function getdownloadid($id)
{
if ($id==1)
 
{
header('Content-Type: application/force-download');
header('Content-Disposition: attachment; filename="downloaded.pdf"');
readfile('http://www.xyz.com/profile1.pdf');
 
//echo "<script type='text/javascript'> document.location.href = 'http://www.xyz.com/thanks.htm'; </script>";
//header("location: http://www.xyz.com/thanks.htm"); 
}
 
elseif ($id==2)
{
header("Content-Type: application/force-download");
header('Content-Disposition: attachment; filename="downloaded.pdf"');
readfile('http://www.xyz.com/profile2.pdf'); 
 
}
 
 elseif ($id==3)
 {
header("Content-Type: application/force-download");
header('Content-Disposition: attachment; filename="downloaded.pdf"');
readfile('http://www.xyz.com/profile3.pdf');
 }
 
 elseif ($id==4)
 {
header("Content-Type: application/force-download");
header('Content-Disposition: attachment; filename="downloaded.pdf"');
readfile('http://www.xyz.com/profile4.pdf');
	
 }
 
 else
 {
 	
 }
 
 }
 
  $id=$_REQUEST['id'] ;
  
  getdownloadid($id);
  echo "<script type='text/javascript'> document.location.href = 'http://www.xyz.com/thanks.htm'; </script>";
 ?>

Open in new window

0
Comment
Question by:sudhir_gs1
6 Comments
 
LVL 5

Expert Comment

by:striker46
ID: 23699647
If the problem is at passing the ID variable, instead of passing the variable as ?id=1 in action, you could also pass it as a $_POST variable. For this you just need to create an extra text field with the attribute "hidden", and set its name and value to be those of the variable you want to pass.

Sample code below.

Then you just retrieve it as $_POST['id']




<input type='hidden' name='id' value="1"/>

Open in new window

0
 
LVL 8

Expert Comment

by:agamal
ID: 23699917
test the code attached .... 3 pages

form1.php
formprint.php
abcd.php


and BTW it can be minimized to form.php and abcd.php only

form1.php
####################################################
<html>
<head>
</head>
<body>
<a href="formprint.php?id=1">Profile 1</a> <br />
<a href="formprint.php?id=2">Profile 2</a> <br />
<a href="formprint.php?id=3">Profile 3</a> <br />
<a href="formprint.php?id=4">Profile 4</a>
</body>
</html>
####################################################
 
printform.php
####################################################
<?php
if($_REQUEST['id']) {
$id=$_REQUEST['id'];
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method='post'>
Name: <input type='text' name='name' /><br />
E-Mail: <input type='text' name='email' /><br />
Phone: <input type='text' name='phone' /><br />
<input type='submit' name="submit" value='submit' />
</form>
<?  } elseif ($_POST['submit'] == "submit") {
?>
<script>
window.open('abcd.php?id="<?php echo $id; ?>"');
</script>
<!-- you thanks.php page code starts here  -->
Thank you !!!!
<!-- you thanks.php page code ends here  -->
<?
}
?>
####################################################
 
abcd.php
####################################################
<?php
 
function getdownloadid($id)
{
header('Content-Type: application/force-download');
header('Content-Disposition: attachment; filename="downloaded.pdf"');
readfile('profile'.$id.'.pdf');
 }
 
if($_REQUEST['id']) {
$id=$_REQUEST['id'];
getdownloadid($id);
}
 ?>
####################################################

Open in new window

0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 750 total points
ID: 23700463
Sorry - I cannot test this, but I think it would do what you want.  Best regards, ~Ray
<?php // RAY_temp_sudhir.php
 
// MAKE SURE WE SEE ALL ERRORS
error_reporting(E_ALL);
ini_set("short_open_tag", TRUE);
 
 
 
// IF NOTHING IN GET OR POST, FIRST TIME ENTRY TO THIS PAGE
if ( empty($_GET) && empty($_POST) ) 
{
// WRITE OUT THE LINKS
   echo "<html>\n\n<head> \n</head>\n<body>\n";
   for ($id=0; $id<4; $id++)
   {
       echo "<a href=\"$PHP_SELF?id=$id\">Profile $id</a><br />\n";
   }
   die( "</body>\n</html>\n" );
}
 
 
 
// IF NO POST DATA YET PUT UP THE FORM
if ( empty($_POST) )
{
   $id = (int)$_GET["id"];
   if ( ($id < 1) || ($id > 4) ) die("INVALID: $id");
?>
<form action="<?=$PHP_SELF?>" method='post'>
        <input type="hidden" name="id"     value="<?=$id?>" />
Name:   <input type='text'   name='name'    /><br />
E-Mail: <input type='text'   name='email'   /><br />
Phone:  <input type='text'   name='phone'   /><br />
        <input type='submit' type='submit' value='submit' />
</form>
<?
   die();
}
 
 
 
// IF POST DATA IS PRESENT
getdownloadid($_POST["id"]);
 
// FUNCTION TO FORCE D/L
function getdownloadid($id)
{
   if ( ($id < 1) || ($id > 4) ) die("INVALID: $id");
 
// FORCE DOWNLOAD
   header('Content-Type: application/force-download');
   header('Content-Disposition: attachment; filename="downloaded.pdf"');
   readfile("http://www.xyz.com/profile" . "$id . ".pdf");
   flush();
 
// REDIRECT   
   header("Location: http://www.xyz.com/thanks.htm");
   exit;
}
?>

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 111

Expert Comment

by:Ray Paseur
ID: 23700518
Here - this is better.  At least I have the parse errors out of the code!  Best, ~Ray
<?php // RAY_temp_sudhir.php
 
// MAKE SURE WE SEE ALL ERRORS
error_reporting(E_ALL);
ini_set("short_open_tag", TRUE);
 
 
 
// IF NOTHING IN GET OR POST, FIRST TIME ENTRY TO THIS PAGE
if ( empty($_GET) && empty($_POST) )
{
// WRITE OUT THE LINKS
   for ($id=1; $id<5; $id++)
   {
       echo "<a href=\"{$_SERVER["PHP_SELF"]}?id=$id\">Profile $id</a><br />\n";
   }
   die();
}
 
 
 
// IF NO POST DATA YET PUT UP THE FORM
if ( empty($_POST) )
{
   $id = (int)$_GET["id"];
   if ( ($id < 1) || ($id > 4) ) die("INVALID: $id");
?>
<form action="<?=$_SERVER["PHP_SELF"]?>" method='post'>
        <input type="hidden" name="id"     value="<?=$id?>" />
Name:   <input type='text'   name='name'    /><br />
E-Mail: <input type='text'   name='email'   /><br />
Phone:  <input type='text'   name='phone'   /><br />
        <input type='submit' type='submit' value='submit' />
</form>
<?
   die();
}
 
 
 
// IF POST DATA IS PRESENT
getdownloadid($_POST["id"]);
 
// FUNCTION TO FORCE D/L
function getdownloadid($id)
{
   if ( ($id < 1) || ($id > 4) ) die("INVALID: $id");
 
// FILE NAME AND DESTINATION
   $filename = "http://www.xyz.com/profile" . "$id" . ".pdf";
   $destinat = "http://www.xyz.com/thanks.htm";
// FORCE DOWNLOAD
   header("Content-Type: application/force-download");
   header("Content-Disposition: attachment; filename=profile$id.pdf");
   readfile($filename);
   flush();
 
// REDIRECT
   header("Location: $destinat");
   exit;
}
?>

Open in new window

0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 23816697
@sudhir_gs:  Please see the grading guidelines here.  I gave you a working example and you marked my answer down to a "B" without any comment or further request for help??  Why did you do that?  This is not the way grading works at EE.

http://www.experts-exchange.com/help.jsp#hi403

Please ask a moderator to change the grade to "A" or please explain why you did not ask for further help.  It costs you nothing more to live within the guidelines of the community!

Thank you for your consideration, ~Ray
0
 

Author Comment

by:sudhir_gs1
ID: 23926524
Ray_Paseur,

Unexpectedly i hit grade B, I am sorry for this. Your solution is good. I am still working on this. Moderator could you please change the grade B to A, by mistake i was chosen wrong. Actually Ray was very helpful.

But I am still having some problem with the below code:

I am just using the below code, but i was unable to redirect to thanks page parallely along with download popup.

Here is my sample code: Do i have to use Output  buffering

header('Content-Type: application/force-download');
header('Content-Disposition: attachment; filename="downloaded.pdf"');
readfile('http://www.xyz.com/profile1.pdf');
 flush();
//echo "<script type='text/javascript'> document.location.href = 'http://www.xyz.com/thanks.htm'; </script>";
header("location: http://www.xyz.com/thanks.htm");
exit;

If it pop's up there wont come thanks page, if thanks page come there wont be download popup.

What I want to do is,
1) I want to download file with thanks page, and i dont want to show my actual web download file name.
2) I want to email the form filled data to admin
3) I want to store the form details in database.

could you please provide the code for all the above in one or two pages
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses
Course of the Month16 days, 21 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question