javascript - HTML 数据列表中的动态数组

标签 javascript php arrays html html-datalist

我想动态刷新此数据列表中的 php 代码,而不重新加载整个页面。

<form id="form1" action="1.5-unterseite-nach-Eingabe.php" method="post" enctype="multipart/form-data">
    <tr>
        <td>Lecture auswählen: </td>
        <td><input list="files" name="unterlage"></td>
    </tr>
    <datalist id="files">
    <?php

        $files = array_diff( scandir("/srv/www/htdocs/share/"), array(".", "..") );
            foreach ($files as $option) {
                echo '<option value=\''.$option.'\'>';
            }
        ?>
    </datalist>

希望你能帮我。

最佳答案

您可以在“index.html”文件中编写 html 表单。并使用javascript间隔请求data.php中PHP提供的数据。伪代码将是这样的:

//index.html

<html>
<form id="form1" action="1.5-unterseite-nach-Eingabe.php" method="post" enctype="multipart/form-data">
<tr><td>Lecture auswählen: </td><td><input list="files" name="unterlage"></td></tr>
<datalist id="files">
</datalist>
</form>
<script>
window.setInterval(function(){
    function request_data(){
           $.ajax({
           url: 'data.php',
           method: "post",
           success: function (data) {
              // Do something here
              $("#files").html(data);
           }
        })
    }
},2000); // 2 secends request a time
</scirpt
</html>

//数据.php

<?php
    $files = array_diff( scandir("/srv/www/htdocs/share/"), array(".", "..") );
    return json_encode($files);
?>

关于javascript - HTML 数据列表中的动态数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53720586/

相关文章:

javascript - 请帮助将水平 css 下拉菜单更改为垂直下拉菜单

javascript - 有没有一种方法可以在不使用各种不同方法的情况下改变字符串?

javascript - 如何将域名映射到 Websocket 服务器(ws)nodejs?

jquery - 如何使用jquery从数组中删除空值

objective-c - Objective-C 中的动态二维 C 数组崩溃

javascript - 如何将变量改成函数名?

php - 如何使用 mysql 和 php 选择多行?

php - 为什么可以从字符串变量实例化类而不是 PHP 中的字符串?

java - 尝试解析数据时 for 循环中的 ArrayIndexOutOfBounds 异常

php - 硬 PHP 螺母 : how to insert items into one associate array?