Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5

Thread: PHP '/suburl&='

  1. #1
    New Coder
    Join Date
    Feb 2010
    Posts
    81
    Thanks
    12
    Thanked 0 Times in 0 Posts

    PHP '/suburl&='

    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?searc...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.

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,467
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    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 Code:
    <?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
    Last edited by mlseim; 02-26-2010 at 06:12 PM.

  • Users who have thanked mlseim for this post:

    TomBower (02-26-2010)

  • #3
    New Coder
    Join Date
    Feb 2010
    Posts
    81
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by mlseim View Post
    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 Code:
    <?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!

  • #4
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,467
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    To make sure my script worked ...
    Here's my test page: http://www.catpin.com/youtube.html

  • Users who have thanked mlseim for this post:

    TomBower (02-26-2010)

  • #5
    New Coder
    Join Date
    Feb 2010
    Posts
    81
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by mlseim View Post
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •