按下提交按钮后,在html页面中显示所选文本文件

Good Day Everyone Ok, so I have done as much as I understand and need some direction and help. Currently i'm very new to html/php so please bear with me. The plan is to list the text files from a Dir in a dropdown list, this I have done, now I would like to display the text file in the same page in a table upon submit button press. This is what I have so far, any input welcome as I am still learning! The bash script is just a grep function to grab specific lines from the original file and copy it to /tmp. Thanks Again

<html>
<head>
<title>Data Request</title>
</head>
<body>
<h1 align="center">Dispatch Report</h1>
<h2 align="center">Wrong Arrives Report</h2>

<table align="center" border="2">
<tr>
<td>Select Shift<br>
<form name="shiftfrm" id="shiftfrm">
<select name="shiftlist" id="shiftlist">
  <option value="" selected="selected">--------</option>
<?php
$dir = opendir ("/var/www/files/");
    while (false !== ($file = readdir($dir))) {
            if (strpos($file, '.txt',1)) {
                echo '<option value="' . $file . '">' . $file . '</option>';
            }
    }
?>
</select>
<input type="submit" id="submit" value="Submit"/>

<?php
if( ($handle = fopen( '/tmp/sh130418n.txt', 'r' )) !== false )
{
$output = '<table align="center" width="" border="2">';
while( ($data = fgetcsv( $handle )) !== false )
{
    $output .= '<tr>';
    foreach( $data as $value )
    {
        $output .= sprintf( '<td>%s</td>', $value );
    }
fclose( $handle );
$output .= '</table>';
}
echo $output;
?>

</td></tr>
</table>

<?php
$output = exec('/var/www/cgi-bin/manualexceptget.sh');
echo "<pre>$output</pre>";
?>

</body>
</html>

assume <form method="post" action="">. Augment your file reading code:

if(!empty($_POST['shiftlist'])) {
    $file = 'files/'.$_POST['shiftlist'];
    if(file_exists($file)) {
        if( ($handle = fopen( $file, 'r' )) !== false )
        {
            $output = '<table align="center" width="" border="2">';
            while( ($data = fgetcsv( $handle )) !== false )
            {
                $output .= '<tr>';
                foreach( $data as $value )
                {
                    $output .= '<td>'.$value.'</td>';
                }
                $output .= '</table>';
            }
            echo $output;
            fclose( $handle );
        }
    }
}

Edit: Fixed the isset() issue stated below. Changed some code, $handle was closed before reading was finished.

Edit2: I just put this in editor and saw many html tags, that were not placed properly (e.g. form not closed). Working sample at pastebin (tested on xampp)

Try Like this for (.txt) files..

<html>
<head>
<title>Data Request</title>
</head>
<body>
<h1 align="center">Dispatch Report</h1>
<h2 align="center">Wrong Arrives Report</h2>

<table align="center" border="2">
<tr>
<td>Select Shift<br />
<form name="shiftfrm" id="shiftfrm" method="POST">
<select name="shiftlist" id="shiftlist">
  <option value="" selected="selected">--------</option>
<?php

$dir = opendir("files/");
while (false !== ($file = readdir($dir)))
{
    if (strpos($file, '.txt', 1))
    {
        echo '<option value="' . $file . '">' . $file . '</option>';
    }
}

?>
</select>
<input type="submit" name="submit" id="submit" value="Submit"/>
<br />
<?php

if (isset($_POST['submit']) && isset($_POST['shiftlist']))
{
    if ($handle = file_get_contents('files/' . $_POST['shiftlist']))
    {
        $output = '<table align="center" width="" border="2">';
        $output .= '<tr><td>';
        echo $handle;
        $output .= '</tr></td>';
        $output .= '</table>';
    } else
    {
        echo "No Content";
    }
} else
{
    echo "Please select the file";
}

?>
</td>
</tr>
</table>
</body>
</html>