Page 1 of 1

open new web browser tab with php

Posted: Sat Nov 05, 2011 12:49 am
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

Re: open new web browser tab with php

Posted: Sat Nov 05, 2011 1:39 am
by social_experiment
I did a quick search and it doesn't look possible using php. Javascript is your other option

Re: open new web browser tab with php

Posted: Sat Nov 05, 2011 2:04 am
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

Re: open new web browser tab with php

Posted: Sat Nov 05, 2011 2:33 am
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

Re: open new web browser tab with php

Posted: Sat Nov 05, 2011 1:38 pm
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

Re: open new web browser tab with php

Posted: Sat Nov 05, 2011 2:09 pm
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.

Re: open new web browser tab with php

Posted: Sat Nov 05, 2011 3:58 pm
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.

Re: open new web browser tab with php

Posted: Sun Nov 06, 2011 2:16 pm
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");

?>