PHP函数不能处理表单提交


PHP Function won't work on form submission

我一直在研究一个从YouTube URL获取ID的函数。函数工作,其中;

Function getYouTubeID($URL){
    $YouTubeCheck = preg_match('![?&]{1}v=([^&]+)!', $URL . '&', $Data);
    If($YouTubeCheck){
        $VideoID = $Data[1];
        Echo $VideoID;
    }
}

唯一不适合我的部分是当我试图在表单提交时调用它;

<form action="<?php If(IsSet($_POST['submit'])){ 
              If(!$_POST['video_id'] == NULL){
                  $VideoID = getYouTubeID($_POST['video_url']); 
                  Header('Location: /watch?v='.$VideoID); }} ?>" method="POST">

我可以在它自己的文件(test.php)中调用函数;

Function getYouTubeID($URL){
    $YouTubeCheck = preg_match('![?&]{1}v=([^&]+)!', $URL . '&', $Data);
    If($YouTubeCheck){
        $VideoID = $Data[1];
        Echo $VideoID;
    }
}
getYouTubeID('http://www.youtube.com/watch?v=example');

这将返回"example",但是,正如前面提到的,我不能通过表单提交调用它,甚至不能通过尝试包含它(包括'filewiththefunction.php')。

任何帮助都是非常感激的,谢谢你的时间。

将检查POST的if条件放在form action中不会做任何事情,您应该做以下事情:

基本示例

form action="someting.php">
<input type="text" name="video_url" value="" />
<input type="submit" name="Show" value="Show" />
</form> 

then in something.php

<?php 
if(isset($_POST['submit'])){
 $VideoID = getYouTubeID($_POST['video_url']); 
 header('Location: /watch?v='.$VideoID); 
}
?>