...

View Full Version : PHP '/suburl&='



TomBower
02-26-2010, 05:49 PM
Okay, so I am practising with PHP and would like a bit of help with creating a youtube custom search. This would include a text box and a submit button.

So, now to the bit I'm a bit confused about. I would like what I typed into the box to be redirected to http://www.youtube.com/results?search_query=the+thing+which+i+searched+for

So could anyone tell me how this is done? I'd really like to learn and all help will be credited. If you don't understand, please post with what you'd like me to expand on.

mlseim
02-26-2010, 06:07 PM
A form on a page somewhere ...

<form action="yt_search.php" method="post">
Search: <input type="text" name="target">
<input type="submit" name="search" value="SEARCH">
</form>

A script called "yt_search.php":


<?php
// where to return to if nothing is entered on the form ...
$return="index.html";

if(isset($_POST['search']) && strlen($_POST['target'])>0){

// get search target from form
$search=$_POST['target'];

// do some things to the search term(s)
$search = strtolower($search); // make it all lower case
$search = str_replace(" ", "+", $search); // change spaces to +

// build the URL for YouTube
$url = "http://www.youtube.com/results?search_query=" . $search;

// go there and make it happen ...
header ("location: $url");
}
else{
// if nothing was entered on the form, return back to the form page.
header ("location: $return");
}
?>



EDIT: fixed the "if" line

TomBower
02-26-2010, 06:14 PM
A form on a page somewhere ...

<form action="yt_search.php" method="post">
Search: <input type="text" name="target">
<input type="submit" name="search" value="SEARCH">
</form>

A script called "yt_search.php":


<?php
// where to return to if nothing is entered on the form ...
$return="index.html";

if(isset($_POST['search']) && strlen($_POST['target'])>0){

// get search target from form
$search=$_POST['target'];

// do some things to the search term(s)
$search = strtolower($search); // make it all lower case
$search = str_replace(" ", "+", $search); // change spaces to +

// build the URL for YouTube
$url = "http://www.youtube.com/results?search_query=" . $search;

// go there and make it happen ...
header ("location: $url");
}
else{
// if nothing was entered on the form, return back to the form page.
header ("location: $return");
}
?>



EDIT: fixed the "if" line
Oh my gosh. Thankyou so much. This really helps..and a well commented piece of php!

mlseim
02-26-2010, 06:15 PM
To make sure my script worked ...
Here's my test page: http://www.catpin.com/youtube.html

TomBower
02-26-2010, 06:21 PM
To make sure my script worked ...
Here's my test page: http://www.catpin.com/youtube.html

Yeah, thanks for that. It works on my localhost aswell so thanks a bunch.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum