【问题标题】:Embedding html in php unexpected <' error [duplicate]在php中嵌入html意外<'错误[重复]
【发布时间】:2019-02-11 19:16:25
【问题描述】:

我是 php 新手,但我无法弄清楚问题所在。我正在尝试为字符串输入制作一个文本框。

<?php
$file = fopen("/file/location","r");
while(!feof($file)){
    echo fgets($file);
    echo "<br>";
}
fclose($file);
<html>
    <form name="form" action="" method="get">
        <input type="text" name="subject" id="subject" value="Car Loan">
    </form>
</html> 
echo $_GET['subject'];
<html> file_put_contents("/file/location", $_GET['subject'], FILE_APPEND); </html>
?>

第一部分运行良好,但是当我到达 &lt;html&gt; 时,它开始吐出各种错误。

【问题讨论】:

  • 你在这里将纯 HTML 注入到 php 中。
  • 另外,您应该会收到未定义的索引通知。

标签: php html embed


【解决方案1】:

正如我在 cmets 中所说,您将纯 HTML 注入 PHP。

您必须关闭 PHP 标记,然后重新打开。

&lt;html&gt; file_put_contents 和相关&lt;/html&gt; 中的&lt;html&gt; 标记也需要删除。

/file/location 的尾部可能应该有一个斜杠,如果这会给您带来问题,但可能不需要,这里只是一个旁注。

<?php   
    $file = fopen("/file/location","r");
    while(!feof($file)){
    echo fgets($file);
    echo "<br>";
    }
fclose($file);
?>

    <html>
            <form name="form" action="" method="get">
            <input type="text" name="subject" id="subject" value="Car Loan">
            </form>
    </html> 

<?php 
    echo $_GET['subject'];
    file_put_contents("/file/location", $_GET['subject'], FILE_APPEND);
?>

但是,您需要在此处使用条件语句,因为您将在 echo'd GET 数组和 file_put_contents 中看到未定义的索引通知,看到您的整个代码都在同一个文件中; action="" 建议。 Error reporting 会在这里为您提供帮助。

即:if(!empty($var)){...} 和/或 if(isset($var)){...}

还要确保该文件夹可以写入并且已为其设置了适当的权限。

您的文件夹声明可能还要求它是完整的服务器路径。

fopen()http://php.net/manual/en/function.fopen.php上查阅手册。

即:fopen("/var/usr/public_html/file/location","r");.

file_put_contents() 也可能发生同样的情况。如果您现在遇到问题,请尝试使用服务器路径。

脚注:

你可以把结尾的&lt;/html&gt;放在

之后
file_put_contents("/file/location", $_GET['subject'], FILE_APPEND);
?>

因为在 HTML 标记之外回显某些内容会在控制台/HTML 源代码中抛出一些内容。

【讨论】:

    【解决方案2】:

    您在没有正确封装的情况下将 html 与 php 混合在一起。 这是一个经过编辑的版本:

    <?php       
        $file = fopen("/file/location","r");
        while(!feof($file)){
            echo fgets($file);
            echo "<br>";
        } 
        fclose($file); 
    ?>
    <html>
        <form name="form" action="" method="get">
           <input type="text" name="subject" id="subject" value="Car Loan">
        </form>
    </html>  
    <?php
        echo $_GET['subject'] . file_put_contents("/file/location", $_GET['subject'], FILE_APPEND); 
     ?>
    

    【讨论】:

    • 这仍然会抛出未定义的索引通知。
    猜你喜欢
    • 2012-03-19
    • 2013-12-28
    • 2012-05-17
    • 2015-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多