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条)

报告相同问题?

悬赏问题

  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集