php - 如何将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>
我真的不知道如何将循环返回的文件添加到组合框()中,以便我可以选择一个特定的并稍后显示它提前谢谢您的帮助
解决方案
你可以像这样在PHP中做到这一点
<?php for ($x = 0; $x <= 10; $x++): ?>
<option value="<?= $x ?>"><?= $x ?></option>
<?php endfor; ?>
在循环的每次迭代中,都会添加一个选项。您可以对数组使用相同的原理
推荐阅读
- r - 应如何将经过训练的 ML 模型合并到使用这些经过训练的模型的包中
- asp.net-core - dotnet Core Authentication OpenIdConnectProtocolException:消息包含错误:OneLogin 后的“invalid_client”
- python - NumPy 在二维数组中切片正方形
- c - C++ 指针增量
- sql-server - SQL Server - 使用生效日期跟踪历史属性,如何忘记(不再有效)属性
- sharepoint - 一个 SharePoint 中的所有根驱动器项目都具有相同的 ID?
- ethereum - 在 Solidity(智能合约/以太坊)中是否有更复杂的函数可以调用?
- python - 将这些数据集中的数组组合成一个 HDF5 文件,最后得到一个 2D
- c++ - C++ ValueError bitset::_M_copy_from_ptr 当字符串和位集的大小正确时
- electron - 如果将电子应用程序上传到服务器,它可以作为网站运行吗?