• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 495
  • Last Modified:

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

0
sudhir_gs1
Asked:
sudhir_gs1
1 Solution
 
striker46Commented:
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
 
agamalCommented:
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
 
Ray PaseurCommented:
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
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
Ray PaseurCommented:
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
 
Ray PaseurCommented:
@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
 
sudhir_gs1Author Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now