首页 > 解决方案 > 我正在尝试搜索和显示数据,如果可能的话,在另一个页面中

问题描述

我试图在我的 .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');
                } 


?>

我想显示数据,如果可能的话,在另一个页面中

标签: php

解决方案


假设这是所有代码,则不会有$_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 中没有其他错误,但这至少应该让您更进一步。


推荐阅读