【问题标题】:PHP inside HTML file - Simple Form and ValidatorHTML 文件中的 PHP - 简单表单和验证器
【发布时间】:2011-11-22 05:19:02
【问题描述】:

我正在处理这个学校项目,但我无法让我的表格正常显示和运行。

This is my School Project

问题/疑问:
1) 我什至可以在 HTML 文档“内部”使用这个 PHP 吗? (The Demo I am Trying to Follow)

这是我的表单条目(在 HTML 文档中)

<div id="formWrap">
        <div id="form">
            <form action="contact.php" method="post" id="comments_form">
        <div class="row">
        <div class="label">Your Name</div>
        <div class="input">
            <input type="text" id="fullname" class="detail" name="fullname" value="<?php echo isset($_POST['fullname'])? $_POST['fullname'] : ''; ?>" />

            <?php if(in_array('fullname', $validation)): ?><span class="error"><?php echo $error_messages['fullname']; ?></span><?php endif; ?>

        </div> <!-- end input class -->
        <div class="context">e.g. John Smith or Jane Doe</div><!-- end context class -->
     </div><!-- end row class -->

     <div class="row">
        <div class="label">Your Email Address</div>
        <div class="input">
            <input type="text" id="email" class="detail" name="email" value="<?php echo isset($_POST['email'])? $_POST['email'] : ''; ?>" />

            <?php if(in_array('email', $validation)): ?><span class="error"><?php echo $error_messages['email']; ?></span><?php endif; ?>

        </div> <!-- end input class -->
        <div class="context">We will not share your email address with anyone.</div><!-- end context class -->
     </div><!-- end row class -->

     <div class="row">
        <div class="label">Tell Us All About It!</div>
        <div class="input">
            <textarea id="comment" name="comment" class="mess">
                <?php echo isset($_POST['comment'])? $_POST['comment'] : ''; ?>
            </textarea>

            <?php if(in_array('comment', $validation)): ?><span class="error"><?php echo $error_messages['comment']; ?></span><?php endif; ?>

        </div> <!-- end input class -->
     </div><!-- end row class -->

     <div class="submit">
        <input type="submit" id="submit" name="submit" value="Send Message" />
        </form>
        <?php else: ?>
            <p>Thank you for your Message!</p>
        <?php endif; ?>

     </div><!-- end submit class -->
     </div><!-- end form -->

        <?php if($form_complete === FALSE): ?>

     </div><!-- end form wrap -->

【问题讨论】:

  • PHP 是否安装在托管该网站的服务器上?否则,PHP 将无法工作。

标签: php html forms contact


【解决方案1】:

PHP 块 (&lt;?php ?&gt;) 必须由 PHP 解析器/解释器处理。这意味着引擎需要知道要读取哪些文件以及要忽略哪些文件。如果这个文件被命名为index.html,PHP 解释器将忽略它(默认情况下,你可以更改它,但这并不是一个常见或推荐的做法)。

您可以正确地将 HTML 放入 PHP 文件中(但在 &lt;?php ?&gt; 块之外),但您无法成功地将 PHP 放入 HTML 文件中。

如果您的文件名为 index.html,请尝试将其重命名为 index.php 并查看您的代码是否有效。

【讨论】:

  • 好的,很高兴知道。我会尝试重做这个并与您联系。谢谢。另外,澄清一下 - html5 在 index.php 场景中工作得很好?
  • 如果 HTML 包含在 PHP 块之外,那么是的,解释器将运行 PHP 并输出 HTML 而不会影响它。
  • 是的,把 index.html 改成 index.php
【解决方案2】:

如果您确实想保留html 扩展名,可以更改服务器配置以将HTML 文件解析为PHP

例如在安装了PHPApache 服务器中,您可以在您的网站目录中创建一个.htaccess 文件并使用AddType 指令:

AddType application/x-httpd-php .html

或者您也可以编辑php.ini 文件。

【讨论】:

    【解决方案3】:

    您要么没有安装 PHP,要么您的 Web 服务器未配置为将 .html 文件传递​​给 PHP。从this page存在的事实来看,PHP已经安装,你可能只需要将你的文件从index.html重命名为index.php,或者重新配置Apache/你的Web服务器。

    【讨论】:

    • @PranavHosangadi - 嗯,当我发布这个答案时,它确实如此,“伙计”。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-16
    • 1970-01-01
    • 2012-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-14
    相关资源
    最近更新 更多