dryb38654 2013-05-06 05:42
浏览 43
已采纳

按下提交按钮后,在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>
  • 写回答

2条回答 默认 最新

  • dousheyan0375 2013-05-06 05:59
    关注

    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)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测