我不知道您打算如何显示 iframe,但是当放置在与表单相同的页面内时,这会起作用:(使用 action="" 作为表单的操作)。
<iframe width="560" height="315" src="https://www.youtube.com/embed/<?php if(isset($_POST['video']) && !empty($_POST['video']) ){ echo $_POST['video']; } ?>" frameborder="0" allowfullscreen></iframe>
使用:
<?php if(isset($_POST['video']) && !empty($_POST['video']) ){ echo $_POST['video']; } ?>
我需要知道您打算如何展示它,以及您在 play.php 文件中的内容。
让我知道这是否适合您。如果您有任何问题,请随时提出。
使用会话
您也可以使用sessions,我认为这是一种更灵活的方法。
这是一个将所有内容都包含在一个页面中的示例,但您几乎可以在该页面之外的任何地方使用会话变量,只要包含 session_start(); 以及会话变量即可。
<?php
session_start();
$_SESSION['video'] = $_POST['video'];
// echo $_SESSION['video']; // for testing purposes only
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Youtube4Tor</title>
</head>
<body>
<center>
<img src="http://twimgs.com/informationweek/byte/news/2012-August/YouTube-Logo.png" />
<h3>Watch YouTube Without Flash in Tor</h3>
<br />
<form action="" method="post">
<table>
<tr>
<td>
Youtube URL:
</td>
<td>
<input type="text" name="video" /><br />
</td>
<td>
<p style="color: #F00">Type in the code after the url,<br /> eg. If the video URl is<br /> "https://www.youtube.com/watch?v=dqUdI4AIDF0",<br /> type in: dqUdI4AIDF0</p>
</td>
</tr>
<tr>
<td colspan="2"><center><input type="submit" /></center></td>
</tr>
</table>
</form>
<iframe width="560" height="315" src="https://www.youtube.com/embed/<?php if(isset($_SESSION['video']) && !empty($_SESSION['video']) ){ echo $_SESSION['video']; } ?>" frameborder="0" allowfullscreen></iframe>
</center>
</body>
</html>
第 2 页测试
相同的视频出现在我创建的另一个页面上(我不必重新输入 YouTube ID),使用以下内容:
<?php
session_start();
// echo $_SESSION['video']; // just to echo the video ID
?>
<iframe width="560" height="315" src="https://www.youtube.com/embed/<?php if(isset($_SESSION['video']) && !empty($_SESSION['video']) ){ echo $_SESSION['video']; } ?>" frameborder="0" allowfullscreen></iframe>
这是另一种使用会话的方法:(我认为是更好的方法)
<?php
session_start();
$video = $_POST['video'] = $_SESSION['video'];
echo $video; // echo video ID number
echo "<hr>";
echo $_SESSION['video']; // echo video ID number test
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Youtube4Tor</title>
</head>
<body>
<center>
<img src="http://twimgs.com/informationweek/byte/news/2012-August/YouTube-Logo.png" />
<h3>Watch YouTube Without Flash in Tor</h3>
<br />
<form action="" method="post">
<table>
<tr>
<td>
Youtube URL:
</td>
<td>
<input type="text" name="video" /><br />
</td>
<td>
<p style="color: #F00">Type in the code after the url,<br /> eg. If the video URl is<br /> "https://www.youtube.com/watch?v=dqUdI4AIDF0",<br /> type in: dqUdI4AIDF0</p>
</td>
</tr>
<tr>
<td colspan="2"><center><input type="submit" /></center></td>
</tr>
</table>
</form>
</center>
</body>
</html>
例如,如果您要创建另一个名为 see_video.php 的页面,您将在该页面上看到相同的视频,而无需重新输入 YouTube ID 号:
<?php
session_start();
if(isset($_SESSION['video']) && !empty($_SESSION['video']))
{
echo $_SESSION['video']; // echo video ID number
}
else { echo "Sorry, no video chosen."; }
?>
<iframe width="560" height="315" src="https://www.youtube.com/embed/<?php if(isset($_SESSION['video']) && !empty($_SESSION['video']) ){ echo $_SESSION['video']; } ?>" frameborder="0" allowfullscreen></iframe>