Form redirect based on part of a form value.

Discussion in 'HTML & Website Design' started by Nintendo, Feb 11, 2010.

  1. #1
    How do you have the redirect URL be based on part of the value of a form? For example,

    <form method="post"><input type="text" name="url" value="http://www.youtube.com/watch?v=WXt0G90iEXw" style="width: 400px"><input type="submit" value="Submit"></form>

    would have the URL that you end up at be

    http://www.domain.com/v/WXt0G90iEXw/
     
    Nintendo, Feb 11, 2010 IP
  2. krsix

    krsix Peon

    Messages:
    435
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Do you want example code for JS or PHP (client vs serverside)?
     
    krsix, Feb 11, 2010 IP
  3. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #3
    If it needs to be in the script, here's the code

    <?php
    $v = $_GET['v'];
    
    echo <<< END
    <title>Title.</title>
    <H1>Blah</H1>
    <form method="post">
    <input type="text" name="url" value="http://www.youtube.com/watch?v=$v" style="width: 400px">
    <input type="submit" value="Get Download Link"></form>
    <hr>
    END;
    
      $tube = new youtube();
      // Only required for restricted videos 
      // $tube->username = "userid";
      // $tube->password = "password";
      $download_link = $tube->get($_POST['url']);
      if($download_link) { ?>
    <P>3. Right click <a href="<?=$download_link;?>">this link</a>.
    <BR>4. Save the file as "newname.flv", with 'newname' being what ever you want the file to be called.
      <?php } else { ?>
        Error locating download URL.  
      <?php } 
    }
    echo <<< END
      <H1>Last 20 Downloads</H1>
    END;
      include('downloads.html');
    ?>
    PHP:

    I tried messing with
    
    <?php if($_SERVER['REQUEST_METHOD'] == 'POST') : $v = $_POST['v'];
    header('Location: [plain]http://www.domain.info/v/[/plain]' . $v . '/'); else:?>
    PHP:
    at the top of the script, but it liked to spit out an error.
     
    Nintendo, Feb 11, 2010 IP
  4. LeetPCUser

    LeetPCUser Peon

    Messages:
    711
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Could it be:

    
    <?php header('location: '.$_POST['url']); ?>
    
    Code (markup):
     
    LeetPCUser, Feb 12, 2010 IP
  5. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #5
    I think I might have a way to get it, by just using the URL to get it, instead of POST using GET on the form.

    $download_link = $tube->get($_GET['url']);
    PHP:
    works if you use the full URL, if it's encoded...

    http://www.domain.com/index.php?url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3D9lp0IWv8QZY

    Though I'm trying to get it to work with

    http://www.domain.com/index.php?url=9lp0IWv8QZY

    so you then only need the '9lp0IWv8QZY' part, making it then possible to have

    http://www.domain.com/v/9lp0IWv8QZY/ . Something like

    $download_link = $tube->get('http://www.youtube.com/watch/v/' $_GET['url']);
    PHP:
    except this gives out errors like

    Parse error: syntax error, unexpected T_VARIABLE in /home/virtual/site68/fst/var/www/html/index.php on line 56
    Parse error: syntax error, unexpected ':' in /home/virtual/site68/fst/var/www/html/index.php on line 56

    depending on if I use " or '.
     
    Nintendo, Feb 16, 2010 IP
  6. krsix

    krsix Peon

    Messages:
    435
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Sorry, forgot about this thread.

    
    // REPLACE _GET WITH SOMETHING ELSE, SANITIZE INPUTS
    preg_match('/http:\/\/(www\.)?youtube(\.[a-z]+){1,2}\/watch\?v=([a-z0-9_-]*)(&([a-z]+)=([a-z0-9_-])+)*?/is', $_GET['url'], $videoid);
    
    $tld = $videoid[2];
    $url = $videoid[0];
    $vid = $videoid[3];
    
    PHP:
     
    krsix, Feb 16, 2010 IP