PHP读取文件或其他文件

时间:2018-05-05 15:14:32

标签: php html isset

我在这里遇到问题,如果我启动这个脚本,服务器没有返回值,我知道这个脚本不是服务器优化的理想选择,但我已经没有任何想法了。 我需要解决方案,如果我从我的选项中选择捷克语或英语,服务器返回正确的值。文件夹在同一个地址簿中。很高兴你的时间和帮助

<select name="example">
<option value="czech" name="czlang" selected="selected">Czech</option>
<option value="eng" name="enlang">English</option>
</select>

<?php
$czfolder = fopen("cz_data.txt", "r");
$enfolder = fopen("en_data.txt", "r");

if(isset($_GET["czlang"])) {
    echo(fread($czfolder, "100"));
      fclose($czfolder); }

if(isset($_GET["enlang"])) {
    echo(fread($enfolder, "100"));
      fclose($enfolder);  
        } 
?>

3 个答案:

答案 0 :(得分:1)

您无法将name attribute放入<option>标记内。请将其放在<select>标记中。

<强>尝试:

<select name="lang">
<option value="czech" <?php ((isset($_GET["lang"]) AND $_GET['lang']) == "czech"?"selected":"");?>>Czech</option>
<option value="eng" <?php ((isset($_GET["lang"]) AND $_GET['lang']) == "eng"?"selected":"");?>>English</option>
</select>

<?php
if(isset($_GET["lang"])) {
    if($_GET["lang"] == "czech"){
        $czfolder = fopen("cz_data.txt", "r");
        echo(fread($czfolder, "100"));
        fclose($czfolder); 
    }
    else if($_GET["lang"] == "eng"){
        $enfolder = fopen("en_data.txt", "r");
        echo(fread($enfolder, "100"));
        fclose($enfolder);  
    }
}
?>

答案 1 :(得分:0)

&#13;
&#13;
<?php
// suggestion: always start with php.  Wait to output 
// anything until you are done processing everything.

// avoid non-existent key on inital run. Take action only on submission.
if(array_key_exists('example', $_GET)) {

  // using a switch statement is purely a matter of personal preference.
  // act on form input, then optionally die before printing form
  $choice = $_GET['example'];
  switch ($choice) {
    case 'czlang':
      print "show Czech...";
      //$czfolder = fopen("cz_data.txt", "r");
      //echo(fread($czfolder, "100"));
      //fclose($czfolder); }
      die;

    case 'enlang':
      print "show English...";
      //$enfolder = fopen("en_data.txt", "r");
      //echo(fread($enfolder, "100"));
      //fclose($enfolder);  
      die;
  }

}

// processing done; output html

?>
<h1>Choose Language</h1>
<form method="get">
  <select name="example">
    <option value="czlang" selected="selected">Czech</option>
    <option value="enlang">English</option>
  </select>
  <input type="submit" value="Display" />
</form>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

问题在于你的fopen()行。 fopen不会在根文件夹中自动搜索。你必须在fopen链接中定义。

$czfolder = fopen("/cz_data.txt", "r");
$enfolder = fopen("/en_data.txt", "r");