【问题标题】:PHP Help, inserting user input into html codePHP 帮助,将用户输入插入 html 代码
【发布时间】:2014-03-08 12:01:06
【问题描述】:

我正在编写一个网站,这样我就可以在没有 Flash 播放器的情况下观看 YouTube。

索引.html > [代码]

<!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="play.php" 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>

[/代码]

我被 PHP 卡住了,你看,我想要它做的是获取用户输入的内容,然后将其插入此处:

<iframe width="560" height="315" src="https://www.youtube.com/embed/**HERE** frameborder="0" allowfullscreen></iframe>

感谢任何帮助。

极光

【问题讨论】:

  • 您将在“S_POST”全局变量中获得用户输入。使用:src="youtube.com/embed/<?phpecho $_POST['video']?>"
  • 除非您将表单发布给自己,否则您无法做到这一点。你最好在 index.html 中包含 play.php 并传递一个局部变量。
  • 需要看看play.php里面有什么
  • 这是 play.php 中的内容 -->

    享受吧。

    视频没有显示?您是否按照说明进行操作?

    我就是这样做的:tor.youtubell.com

标签: php html input


【解决方案1】:

我不知道您打算如何显示 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>

【讨论】:

  • 感谢您的反馈。我就是这样做的。 tor.youtubell.com 这就是 play.php 中的内容 --->

    享受吧。

    视频没有显示?您是否按照说明进行操作?

  • 不客气。我回来时很乐意提供帮助,我现在必须离开,但我回来时会与您联系。 @Aurora
  • 嗨。当我在您的网站上尝试时效果很好。 @Aurora
  • 如果您觉得我的回答解决了您的问题,请单击白色复选标记直到它变为绿色以接受我的回答。 @Aurora
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-04
  • 2013-03-31
  • 2020-02-20
  • 2016-10-21
  • 2012-03-28
相关资源
最近更新 更多