php - 我正在尝试搜索和显示数据,如果可能的话,在另一个页面中
问题描述
我试图在我的 .txt 文件中显示数据,我的数据没有显示,也没有错误消息。请帮忙!
<form method="post" action="">
<input type="text" name="carplate"/>
<input type="submit" name="submit"/>
</form>
<?php
if(!empty($_POST['carplate'])) {
$file = 'carListingDB.txt';
$searchfor = '';
// the following line prevents the browser from parsing this as HTML.
header('Content-Type: text/plain');
$searchfor = $_POST['carplate'];
$contents = file_get_contents($file);
$pattern = preg_quote($searchfor, '/');
$pattern = "/^.$pattern.\$/m";
if(preg_match_all($pattern, $contents, $matches)){
echo "Found matches:\n";
echo implode("\n", $matches[0]);
echo file_get_contents( "carListingDB.txt" );
}
else{
echo "No matches found";
}
header('Content-Type: text/html');
}
?>
我想显示数据,如果可能的话,在另一个页面中
解决方案
假设这是所有代码,则不会有$_POST['carplate']
. 你也没有在你的 PHP 代码周围放置 php 标签
$_POST
尝试在全局中查找提交按钮数组元素。
<form method="post" action="">
<input type="text" name="search"/>
<input type="submit" name="submit"/>
</form>
<?php
if(!empty($_POST['submit'])) {
$file = 'carListingDB.txt';
$searchfor = '';
// the following line prevents the browser from parsing this as HTML.
header('Content-Type: text/plain');
$searchfor = $_POST['carplate'];
$contents = file_get_contents($file);
$pattern = preg_quote($searchfor, '/');
$pattern = "/^.$pattern.\$/m";
if(preg_match_all($pattern, $contents, $matches)){
echo "Found matches:\n";
echo implode("\n", $matches[0]);
echo file_get_contents( "carListingDB.txt" );
}
else{
echo "No matches found";
}
header('Content-Type: text/html');
}
?>
我不能保证在您的实际 PHP 中没有其他错误,但这至少应该让您更进一步。
推荐阅读
- angular - 将值从 app.config 文件传递给组件
- math - 皮亚诺公理如何只构造自然数集而不构造其他集合
- amazon-web-services - AWS DynamoDB 创建数据库
- node.js - 使用spread和所有函数axios不加载类函数
- adobe-reader - 保护模式下的 AppContainer 不兼容 - Adobe Acrobat Reader
- excel - Excel 在运行宏时一直冻结
- c++ - c ++错误:'Matrix *'和'Matrix *'类型的无效操作数到二进制'operator +'
- c# - 是 IHubContext
SignalR 线程安全? - here-api - 使用 Geocoder API 搜索邮政信箱
- python - Timeline of all changes to piece of code to revert back