1

我的页面有一个文本输入和一个按钮,用户必须在文本字段中填写一个引用可播放媒体文件(mp4)的 URL,然后单击按钮

单击按钮时,脚本正在运行,并且 ajax 将 url 值传递给 PHP 文件,该文件进行一些验证并将响应发回

如果一切正常,它应该用 JW 播放器替换 DIV 标签,用户可以播放媒体

我通过用纯文本或图像替换DIV标签来测试整个过程,它运行顺利

当我尝试用 jw 播放器组件替换它时,它没有显示任何东西

我通过将它直接插入到 HTML 代码中来测试 jw 并且它运行顺利

这是完整的 index.html

    <!DOCTYPE html>
    <html>
    <head>
    <script type="text/javascript" src="/ajax4/jwplayer.js"></script>
    <script type="text/javascript">jwplayer.key="RXVe0CXFsIS8pAar5ezgSLJqb9jfx7rDOBxkVw==";</script>
    <script type="text/javascript"><!--

    function get_XmlHttp() {
      var xmlHttp = null;

      if(window.XMLHttpRequest) {       // for Forefox, IE7+, Opera, Safari, ...
        xmlHttp = new XMLHttpRequest();
      }
      else if(window.ActiveXObject) {   // for Internet Explorer 5 or 6
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
      }

      return xmlHttp;
    }

    // sends data to a php file, via POST, and displays the received answer
    function ajaxrequest(php_file, tagID) {
      var request =  get_XmlHttp();     // call the function for the XMLHttpRequest instance

      // create pairs index=value with data that must be sent to server
      var hiturl = document.getElementById('hit').value;
      var  the_data = 'hiturl='+hiturl;

      request.open("POST", php_file, true);         // set the request

      // adds  a header to tell the PHP script to recognize the data as is sent via POST
      request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      request.send(the_data);       // calls the send() method with datas as parameter

      // Check request status
      // If the response is received completely, will be transferred to the HTML tag with tagID
      request.onreadystatechange = function() {
        if (request.readyState == 4) {
          document.getElementById(tagID).innerHTML = request.responseText;
        }
      }
    }
    --></script>

    </head>
    <body>
    <form action="test_form.php" method="post" name="form1" onsubmit="ajaxrequest('hitphp.php', 'myDiv'); return false;">
    <label for="name" id="name_label">hit URL: </label>  
    <input type="text" name="hit" id="hit" size="100"  /> 
    <input type="submit" value="Enter hit UrL" />

    </form>
    <div id="myDiv"></div>

    </body>
    </html>

和 hitphp.php 文件

    <?php
    // if data are received via POST
    if (isset($_POST['hiturl'])) {
      // get data into variables, deleting the html tags
      $hiturl = strip_tags($_POST['hiturl']);

      // if the form fields are completed
      if (true) {
        if (true) {


    echo'<div id="myElement">nnnnn.....</div><script type="text/javascript">
jwplayer("myElement").setup({

    type:"mp4", 
    bufferlength: "60",
    file: "mediaurl",


});


</script>';



}else{
echo 'sorry not a valid url';
}
      }
      else {
        echo 'Fill in all form fields';
      }
    }
    ?> 
4

0 回答 0