dongsong4418 2015-09-15 16:52
浏览 107
已采纳

添加项目到数组而不刷新页面?

http://alpha.ripfy.com/

As seen in the following demo, I have a YouTube video that I want to be able to play while adding items to the array in PHP. Sadly, this isn't possible from what I've tried because the page refreshes every time I add an item to the array.

Would there be any way of achieving this without the page refreshing (forcing the video to restart?)

Code:

<?php
    if (isset($_POST['playlist'])) {
        $playlist = $_POST['playlist'];
    } else { // Else set my default list
        $playlist = array("Be more.mp3", "Drift Away.mp3", "Panda Sneeze.mp3");
    }

    if (isset($_POST['name1'])) {
        $playlist[] = $_POST['name1'];
    }
?>

<form method="post">

<?php
    foreach($playlist as $song) {  
?>

<input type="hidden" name="playlist[]" value="<?php echo $song?>">

<?php
    }
?>

<input type="text" name="name1"/>
<input type="submit" name="submit1"/>
</form>

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

<?php
    foreach ($playlist as $value) {
        echo $value."<br>";
    }
?>

Thanks for helping me out!

  • 写回答

4条回答 默认 最新

  • douluo1330 2015-09-15 18:47
    关注

    Here's my answer. That fully works. In this script, the server checks the presence of a name1 request (post or get). If exists, it returns the string posted (only) and if doesn't, it returns what already was there. And the post() method posts (gets) the data and appends to the container using innerHTML+= data + "<br>"; evaluate the code, it should be self explanatory.

    <?php
    if (isset($_REQUEST['playlist'])) {
        $playlist = $_REQUEST['playlist'];
    } else { // Else set my default list
        $playlist = array("Be more.mp3", "Drift Away.mp3", "Panda Sneeze.mp3");
    }
    
    if (isset($_REQUEST['name1'])) {
       // if exists, return plain text responce. NOT HTML
        $playlist[] = $_REQUEST['name1'];
        echo $_REQUEST['name1'];
    }
    else{
    
    ?>
    <html>
    <head>
    <title>Demo</title>
    <script src="jquery.js"></script>
    
    </head>
    <body>
    
    <script>
    text= ""
    
    function onUpdate(){
        text = document.getElementById("name1").value;
    }
    
    function handler(data){
         document.getElementById('container').innerHTML += data+"<br>";
    }
    
    function post(){
        onUpdate();
        $.get("index.php", name1="+text, handler);
     }
    </script>
    
    <input type="text" name="name1" id="name1"/>
    <input type="submit" name="submit1" onclick="post()"/>
    
    <iframe width="560" height="315" src="https://www.youtube.com/embed/pzB6CxChIQk" frameborder="0" allowfullscreen></iframe>
    <br>
    <div id="container">
    <?php
        foreach ($playlist as $value) {
            echo $value."<br>";
        }
    ?>
    </div>
    </body>
    <?php  };?>
    

    Although, it works as intended, i don't see the point of using it. just plain js should work

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?