open new web browser tab with php

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
rajohns08
Forum Newbie
Posts: 4
Joined: Sat Nov 05, 2011 12:44 am

open new web browser tab with php

Post by rajohns08 »

i want to get a user to fill out an html text form and then use that variable to open up a new tab and take them to a google books search page of the title they entered.

so my php code looks like:

Code: Select all

<?php
$title = $_POST["title"];
$title = str_replace(" ", "+", $title);
$homepage = file_get_contents("https://www.google.com/search?q=$title&btnG=Search+Books&tbm=bks&tbo=1");
echo $homepage;
?>
however there is a problem with this code because it only displays the contents of the page i direct it to. this means that the user cannot click any links on the page that is displayed. how can i correct this problem?

EDIT: i can also use window.open but i would really prefer if it would just open a new tab
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: open new web browser tab with php

Post by social_experiment »

I did a quick search and it doesn't look possible using php. Javascript is your other option
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
rajohns08
Forum Newbie
Posts: 4
Joined: Sat Nov 05, 2011 12:44 am

Re: open new web browser tab with php

Post by rajohns08 »

i can't even find a way to do it in javascript. window.open is javascript but i can't find one like tab.open
User avatar
flying_circus
Forum Regular
Posts: 732
Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR

Re: open new web browser tab with php

Post by flying_circus »

Have you tried adding the "target" attribute to your form?

I hate linking to w3fools, but here is an example
http://www.w3schools.com/tags/att_form_target.asp
rajohns08
Forum Newbie
Posts: 4
Joined: Sat Nov 05, 2011 12:44 am

Re: open new web browser tab with php

Post by rajohns08 »

flying_circus wrote:Have you tried adding the "target" attribute to your form?

I hate linking to w3fools, but here is an example
http://www.w3schools.com/tags/att_form_target.asp
but if i use this way then how will i pass my book title variable into the url that i want to open? because the url that i want to open is dynamic dependent on the input they type in
nowaydown1
Forum Contributor
Posts: 169
Joined: Sun Apr 27, 2008 1:22 am

Re: open new web browser tab with php

Post by nowaydown1 »

Expanding on the idea presented by flying_circus a bit:

You could change your form target to a page that you create. Maybe like openlink.php. Set your form target to _blank like flying_circus presented. Then with your form data comes in, do your str_replace like you are doing now. Build your URL string, save it in a variable, then issue a header redirect to kick the user to where you want them to go.

I haven't tested this, but in theory openlink.php should open in a new tab, do it's form processing and kick the user on. Might be worth a shot.
User avatar
flying_circus
Forum Regular
Posts: 732
Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR

Re: open new web browser tab with php

Post by flying_circus »

nowaydown1 wrote:Expanding on the idea presented by flying_circus a bit:

You could change your form target to a page that you create. Maybe like openlink.php. Set your form target to _blank like flying_circus presented. Then with your form data comes in, do your str_replace like you are doing now. Build your URL string, save it in a variable, then issue a header redirect to kick the user to where you want them to go.

I haven't tested this, but in theory openlink.php should open in a new tab, do it's form processing and kick the user on. Might be worth a shot.
Sure, that should work, you're just creating a forwarder.
rajohns08
Forum Newbie
Posts: 4
Joined: Sat Nov 05, 2011 12:44 am

Re: open new web browser tab with php

Post by rajohns08 »

nowaydown1 wrote:Expanding on the idea presented by flying_circus a bit:

You could change your form target to a page that you create. Maybe like openlink.php. Set your form target to _blank like flying_circus presented. Then with your form data comes in, do your str_replace like you are doing now. Build your URL string, save it in a variable, then issue a header redirect to kick the user to where you want them to go.

I haven't tested this, but in theory openlink.php should open in a new tab, do it's form processing and kick the user on. Might be worth a shot.
thanks you are the man! here is my solution code:

Code: Select all

<?php

$title = $_POST["title"];
$title = str_replace(" ", "+", $title);

header("Location: http://www.amazon.com/s/ref=nb_sb_noss?url=search-alias%3Dstripbooks&field-keywords=$title&x=0&y=0");

?>
Post Reply