将值从php传递给html

时间:2016-09-25 12:57:01

标签: php html

我想将我的PHP代码(在同一页面中)传递给我的html - 所以我可以很好地打印出来

这是我的php函数(它暂时需要从文本中打印出3行)

    if(sizeof($words)==1)
    {
        $single = new single;
        $docres = $single->find($words);
            $dir = "source";
            foreach ($docres as $key=>$filename) {
                $counter = 0; 
                $file = $filename +1;
                $handle = fopen($dir."/".$file.'.txt',"r");
                if($handle)
                {
                    while($counter < 3)
                    {
                        echo fgets($handle);
                       //this is what i need to print in my html
                        $input = $_POST['input'];
                        $counter++;
                }
            }
    }
}

这是我的HTML

<html>
    <head> 
        <title>serach engine</title> 
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    </head> 
    <body>
        <form action="" method="POST">
            <center> 
                <h1> My Search Engine </h1>
                <input type = 'text' size='90'  value='' name = 'search' > <br> 
                <input type = 'submit' name = 'submit' value = 'Search source code' >         
               // <input type="value" name="input" value="<?php echo $input; ?>">
            </center> 
        </form >
      //I would like to print it out here
        </div>
    </body > 
</html >

我在网上搜索并看到了使用$ _POST的解决方案,但它对我不起作用......

1 个答案:

答案 0 :(得分:1)

你可以将它附加到一个新变量并在html中回显它。

例如:

while($counter < 3) {
     echo fgets($handle);
    //this is what i need to print in my html
    $input .= $_POST['input'] . "<br />";
    // You could also do:
    // $input .= "<li>" . $_POST['input'] . "</li> "
    // For a list item
    $counter++;
}

然后在html中回复它:

</form>
  <?php echo $input; ?>
</div>

注意:您没有在$counter循环中增加while