dtrovwl75780 2014-03-08 12:01
浏览 35
已采纳

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

  • 写回答

1条回答 默认 最新

  • dua27031 2014-03-08 14:55
    关注

    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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 我的数据无法存进链表里
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端