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 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来