PHP帮助,将用户输入插入到html代码中

I'm coding a site so that I can watch YouTube without flash player.

index.html > [code]

<!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>

[/code]

I'm stuck on the PHP, you see, what I want it to do is to get what the user entered, and insert it here:

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

Any help is appreciated.

Aurora

I don't know how you're planning on showing the iframe, but this works when placed inside the same page as the form: (Using action="" as the form's 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>

Using:

<?php if(isset($_POST['video']) && !empty($_POST['video']) ){ echo $_POST['video']; } ?>

I would need to know how you're planning to show it, and what you have inside the play.php file.

Let me know if this works for you. If you have any questions, feel free to ask.


Using sessions

You can also use sessions which I tend to think would be a more flexible method.

Here is an example which has everything inside one page, but you could use the session variable pretty much anywhere outside that page, just as long as session_start(); is included, along with the session variable.

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

Page 2 test

The same video showed up on another page that I created (I didn't have to re-enter the YouTube ID), using the following:

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

Here is another method using sessions: (A better method, I think)

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

Then if you were to create another page called see_video.php for example, you would see the same video on that page without re-entering the YouTube ID number:

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