We help IT Professionals succeed at work.

How to open a new browser tab after executing php script

Black Sulfur
Black Sulfur asked
on
8,581 Views
1 Endorsement
Last Modified: 2017-05-08
I am using a php to PDF library to generate a pdf document once a user has submitted data and once a user inserts a record I want another tab to open with the pdf and then redirect the initial page they were on back to the dashboard.

The below doesn't work as I am just redirected to the dashboard and it is code for opening a new window, not tab. It doesn't even open a new window and even if it did, it would probably be blocked by a pop-up blocker.

Any suggestions?

//insert code goes here. After success...
echo "<script type='text/javascript' language='Javascript'>window.open('pdf/examples/pdf.php');</script>";
header("location: dashboard.php");
exit();

Open in new window

Comment
Watch Question

Most Valuable Expert 2011
Author of the Year 2014
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Great idea, thanks!
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Most Valuable Expert 2011
Author of the Year 2014
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Dave BaldwinFixer of Problems
CERTIFIED EXPERT
Most Valuable Expert 2014

Commented:
In my experience, only the browser user can choose whether a page opens in a new tab or new window.  I have never seen any code that could choose between them.

Author

Commented:
Thanks for the suggestions. I did consider forcing a download but I don't really want the user to download them, but more view online only and download if they want to.
Dave BaldwinFixer of Problems
CERTIFIED EXPERT
Most Valuable Expert 2014

Commented:
You do realize (I hope) that to view the PDF files in your browser you are actually downloading them to a 'temp file' on your computer and opening them locally?  That's true even of HTML files.  Everything thing you see in your browser is at least for that moment downloaded to your computer.
Most Valuable Expert 2011
Author of the Year 2014

Commented:
I don't really want the user to download ...
It's not our choice on the server side of things.  If you create a document that the client can see, you have created a document that the client has already downloaded  before seeing it.  At that point it's up to the client to decide whether they want to store the document, delete the document, send the document to Wikileaks, publish the document on another web site, etc.

Author

Commented:
To clear things up, what I meant was that I don't want to force a download as it is unnecessary. I just wanted it to open in a new tab but I can't so I will do what Ray suggested and provide them a link to the document. They are welcome to download the PDF if they want,  to a location of their choice by clicking on the default download icon once the PDF is presented to them or they can just print it. That would be easier than going through the temp folder trying to locate it.

My goal was just to get them to print it and issue it but if they want to save them to keep a record then that is fine with me as well.

Commented:
You know most browsers open PDF's in the browser anyway now so you're kinda of fighting against the grain wanting to do it your way

Author

Commented:
I am not actually sure if a temp PDF is downloaded. If you are interested you can try it yourself. I used this:

https://tcpdf.org

You actually go to a .php extension to view the PDF. So, that is different to going to mysite.com/pdf/pdf.pdf

You are actually just going to mysite.com/pdf/pdf.php and being rendered a pdf.

Author

Commented:
Or I could be wrong. But it is doing what I want it to do which is take info from my database and show it in PDF format in a webpage.

Commented:
Even tho its a php page it's still a PDF that is stored locally
Most Valuable Expert 2011
Author of the Year 2014

Commented:
I am quite sure that anything at all - web page, image, video, PDF, anything that can be displayed on the client browser has been downloaded and stored on the client computer or device.  It has been released into the wild.
Dave BaldwinFixer of Problems
CERTIFIED EXPERT
Most Valuable Expert 2014

Commented:
Everything thing you see in your browser is at least for that moment downloaded to your computer.

Author

Commented:
But is it stored locally as a pdf or webpage?

Commented:
PDF as you will have sent PDF headers to tell the browser this is a PDF
Dave BaldwinFixer of Problems
CERTIFIED EXPERT
Most Valuable Expert 2014

Commented:
Whatever the file is, it is saved in a temp directory and then opened locally.  That is the way that web browsers have Always worked.  The current location in my Firefox is C:\Documents and Settings\dib2\Local Settings\Application Data\Mozilla\Firefox\Profiles\rouzq6h0.default-1453017288171\cache2\entries and it has 34,915 entries for the last year and a half.  It is the browser 'cache'.

Author

Commented:
Haha, that's a lot!
Dave BaldwinFixer of Problems
CERTIFIED EXPERT
Most Valuable Expert 2014

Commented:
That's just Firefox.  I didn't even look at Chrome or SeaMonkey.  Or on any of the other computers...  Note that every little or large file that gets downloaded is kept in the 'cache'.  Files from less than 1KB to 9599 KB.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.