首页 > 解决方案 > 如何将html与php结合起来

问题描述

我正在尝试将 php 与 html 表单连接起来。目前我的代码如下所示:

<? php

$path = "books /";
$books = opendir ($path);
while (($book = readdir ($books))! == false)
{
    if (substr ($book, -4) === ".txt")
    {
        $filePath = $path. $book;
        // echo $book;

        $readFile = fopen ($filePath, "r+") or die ("Permission error");
        $infoBook = fread ($readFile, filesize($filePath));
        echo $infoBook;
        echo "<br>";
        $writeInfo = fwrite ($readFile, filesize($filePath));
        fclose ($readFile);
    }
    
}
closedir ();

?>
<html>
    <head>
        <title> Books Form </title>
        <meta charset = "UTF-8">
        <meta name = "viewport" content = "width = device-width, initial-scale = 1.0">
        <script src = "jquery-3.6.0.js"> </script>
        <style>
            
        </style>
    </head>
    <body>
        <form method = "post" action="<? php $_SERVER ['PHP_SELF'];?>" id = "bookform">
            <label for = "books"> </label>
            <select name = "book" id = "books" form = "bookform">
                
                <option value = "blank"> </option>
                <option value = "Atlas_chmur.txt"> Cloud Atlas </option>
                <option value = "Dune.txt"> Dune </option>
            </select> <br> <br>
            <button style = "position: relative; left: 240px" type = "submit" form = "form" value = "<? php $readFile = fopen ($book," r + ");?>" name = "click" > Load </button> <br> <br>
            <textarea name = "info" rows = "30" cols = "100">
                <? php echo $infoBook?>
            </textarea> <br>
            <button type = "submit" form = "form" value = "Submit"> Save </button> <br>
        </form>
    </body>
</html>

我真的不知道如何将循环返回的文件添加到组合框()中,以便我可以选择一个特定的并稍后显示它提前谢谢您的帮助

标签: phphtml

解决方案


你可以像这样在PHP中做到这一点

<?php for ($x = 0; $x <= 10; $x++): ?>
  <option value="<?= $x ?>"><?= $x ?></option>
<?php endfor; ?> 

在循环的每次迭代中,都会添加一个选项。您可以对数组使用相同的原理


推荐阅读