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 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)