2013-05-06 141 views
0

美好的一天所有人 好的,所以我尽我所能做了很多,需要一些指导和帮助。目前我对html/php很新,所以请耐心等待。计划是在下拉列表中列出来自Dir的文本文件,这是我已经完成的工作,现在我想在提交按钮按下时在表格的同一页中显示文本文件。这是我到目前为止,任何输入欢迎,因为我仍然在学习! bash脚本只是一个grep函数,用于从原始文件中获取特定行并将其复制到/ tmp。 再次感谢提交按钮后,在html页面显示选定的文本文件

<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> 

回答

0

承担<form method="post" action="">。增加你的文件阅读代码:

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); 
     } 
    } 
} 

编辑:修复isset()问题,如下所述。更改了一些代码,在读完之前关闭了$ handle。编辑2:我只是把它放在编辑器中,看到很多html标签,没有正确放置(例如表单未关闭)。由于使用了`!empty`你不需要`isset`工作在pastebin样本(在XAMPP测试)

+0

。 – 2013-05-06 06:03:56

+0

尝试对不存在的变量使用[empty](http://php.net/empty)。它会抛出一个错误。 [isset](http://php.net/isset)阻止了这一点,因为它在isset函数返回false的那一刻结束了if语句。 – Imperative 2013-05-06 06:07:02

+0

“确定一个变量是否被认为是空的,如果一个变量不存在或者它的值等于FALSE,则该变量被认为是空的,如果该变量不存在,empty()不会产生警告。来自'empty()'的PHP手册。 http://us3.php.net/manual/en/function.empty.php – 2013-05-06 06:11:03

0

尝试像这样文件(.txt)..

<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> 
+0

感谢MIL,它的工作原理是将逗号分隔的txt文件转换为coloums和rows,只显示文件。但我更近了一步! – kirchner20 2013-05-06 06:44:32

+0

仍然需要一些帮助,无法使用颜色显示文件,任何人都可以指向正确的方向吗?谢谢@Prasath Albert – kirchner20 2013-05-06 08:41:13

相关问题