Link to home
Start Free TrialLog in
Avatar of sudhir_gs1
sudhir_gs1

asked on

Problem in redirection after download in php

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

Avatar of striker46
striker46
Flag of Germany image

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

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

ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

@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.

https://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
Avatar of sudhir_gs1
sudhir_gs1

ASKER

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