我正在尝试搜索和显示数据,如果可能,请在另一页中显示

时间:2019-02-01 16:26:13

标签: 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');
                } 


?>

我想在其他页面中显示数据

3 个答案:

答案 0 :(得分:0)

假设这是所有代码,则不会有$_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中没有其他问题,但这至少可以使您更进一步。

答案 1 :(得分:0)

我已经重新整理了一下您的代码。我已经更改了preg_match_all()

的模式
?php
if (!empty($_POST['carplate'])) {
    $file = 'carListingDB.txt';
    $searchfor = '';
    $searchfor = $_POST['carplate'];
    $contents = file_get_contents($file);
    $pattern = preg_quote($searchfor, '/');
    $pattern = "/^.*${pattern}.*\$/mi";
    if (preg_match_all($pattern, $contents, $matches)) {
        header('Content-Type: text/plain');
        echo "Found matches:\n";
        echo implode("\n", $matches[0]);
        echo "\n\n";
        echo "File's content:\n";
        echo file_get_contents($file);
        exit;
    } else {
        echo "No matches found";
    }
}
?>
<form method="post" action="" target="_blank">
  <input type="text" name="carplate"/>
  <input type="submit" name="submit"/>
</form>

文件 CarListingDB.txt

的示例内容
audi a6
audi a4
audi mini
escudo v6
escudo v4
vitara

成功请求的一个示例-audi

答案 2 :(得分:0)

我设法通过更改以下内容来显示结果

$pattern = "/^.$pattern.\$/m";

$pattern = "/^.*$pattern.*\$/m";