如何使用单选按钮输入从文件中选择性打印



我正在尝试制作一个单页PHP页面,该页面从用户之前选择的单选按钮中读取,并使用该单选按钮搜索文件并打印与该文件信息槽对应的信息,但我无法将其实际打印出来。我不知道是不是只是它没有正确读取文件,没有正确搜索文件,或者其他任何可能错误的事情,所以如果这是非常明显的事情,请提前道歉。文件的每一行都有4位信息,所有信息之间都有逗号:位置、位置类型和两个数字。这是第一行:罗阿坦,自然礁,28,41。我正试图使用第一个位置来搜索,因为这就是单选按钮的基础。我希望这是足够的信息。

<input type="radio" name"site" value="Roatan"> Roatan <br /></input>

这就是单选按钮的格式,它与提交按钮都在一个表单标记中。

foreach ($dives as $i=>$record) {
$dives = explode(",", $record);
if (($site == "Roatan") and ($record[0] == "Roatan")) {
print "<tr>
<td> $dives[0] <br /></td>
<td> $dives[1] <br /></td>
<td> $dives[2] <br /></td>
</tr>";
$i++;
}
}

这就是循环和条件语句的样子。

根据注释,您有一个拼写错误。

不过,这应该会让你开始。

示例脚本:

使用php -S localhost:8000 index.php从命令行运行,并使用从浏览器访问http://localhost:8000.

<?php
// filename: index.php
$chosen_site = isset($_POST['site']) ? trim($_POST['site']) : null;
$file = "Roatan,Natural Reef,28,41n Some,Thing,32,42n Value1, Test, 89, 90";
$results = "";
$lines = explode("n", $file);
?>
<html>
<head>
<style>
.wrapper {
margin: 0 auto;
padding: 20px;
width: 900px;
/*text-align: center;*/
border: 1px solid black;
}
.fail {
color: red;
}
.pass {
color: green;
}
</style>
</head>
<body>
<div class="wrapper">
<h1>Results</h1>
<hr/>
<p><b>You Chose:</b><?= json_encode($chosen_site) ?></p>
<?php
// For each line of the file.
foreach ($lines as $line) {
//Print result of each line:
if(strpos($line, $chosen_site) !== false) { 
echo '<p class="pass"><b>Match found:</b>';
echo '<ul>';
$parts = explode(',', $line);
foreach ($parts as $part) {
echo '<li>'. $part . '</li>';
}
echo '</ul>';
echo '</p>';
} else {
echo '<p class="fail"><b>Match not found: </b>' . $line . '</p>';
}
}
?>

<h1>Form</h1>
<hr/>
<form method="post">
<input type="radio" name="site" value="Value1">Value1</input>
<input type="radio" name="site" value="Value2">Value2</input>
<input type="radio" name="site" value="Value3">Value3</input>
<br/>
<br/>
<input type="submit"/>
</form>
</div>
</body>
</html>

最新更新