HTML表单显示代码而不是PHP输出

时间:2016-03-27 11:39:53

标签: php html forms twitter-bootstrap

我一直在尝试将此表单实现到我的HTML(Bootstrap 4 alpha)网站中,但我无法正常工作。

让我们从目标开始:

  1. 一种工作表单,用于检查在发送电子邮件之前是否为所有字段指定了有效输入。
  2. 如果用户没有为一个或多个字段输入有效输入,则页面本身应显示错误消息(因此不会重定向到错误页面)。
  3. 最好,当用户输入某个输入字段时出错,并显示错误时,先前输入的数据应保留在字段中。
  4. 我在互联网上找到了一些有用的帮助,PHP和HTML,我可以使用我需要的东西。但是,即使我上传原始代码而不更改任何内容,此表单也不起作用。

    我认为我上传它的服务器可能不支持PHP,但它确实是,我甚至得到一个表单工作,只需要输入并将其发送到接收器,没有问题。

    我没有开始工作的是在我的HTML中回显我的PHP文件的输出。我做错了什么?

    HTML:

    <form action="php/mail.php" role="form" method="post" class="">
        <div class="form-group">
            <label for="name" class="col-sm-2 control-label sr-only">Naam</label>
            <input type="text" class="form-control form-control-sm form" id="name" name="name" placeholder="Name" value="<?php echo htmlspecialchars($_POST['name']); ?>">
            <?php echo "<p class='text-danger'>$errName</p>";?>
        </div>
        <div class="form-group">
            <label for="email" class="col-sm-2 control-label sr-only">Email</label>
            <input type="email" class="form-control form-control-sm form" id="email" name="email" placeholder="your.name@example.com" value="<?php echo htmlspecialchars($_POST['email']); ?>">
            <?php echo "<p class='text-danger'>$errEmail</p>";?>
        </div>
        <div class="form-group">
            <label for="message" class="col-sm-2 control-label sr-only">Message</label>
            <textarea class="form-control form-control-sm form" rows="4" name="message" placeholder="My Message..."><?php echo htmlspecialchars($_POST['message']);?></textarea>
            <?php echo "<p class='text-danger'>$errMessage</p>";?>
        </div>
        <div class="form-group">
            <label for="human" class="col-sm-2 control-label sr-only">2 + 3 = ?</label>
            <input type="text" class="form-control form-control-sm form" id="human" name="human" placeholder="2 + 3 = ?">
            <?php echo "<p class='text-danger'>$errHuman</p>";?>
        </div>
        <div class="form-group">
            <input id="submit" name="submit" type="submit" value="Send" class="btn btn-primary btn-sm">
            <input type="reset" value="Clear" class="btn btn-primary btn-sm">
        </div>
        <div class="form-group">
            <?php echo $result; ?>
        </div>
    </form> 
    

    PHP:

    <?php
        if (isset($_POST["submit"])) {
            $name = $_POST['name'];
            $email = $_POST['email'];
            $message = $_POST['message'];
            $human = intval($_POST['human']);
            $from = 'Demo Contact Form'; 
            $to = 'test@testmail.com'; 
            $subject = 'Message from Contact Demo ';
    
            $body = "From: $name\n E-Mail: $email\n Message:\n $message";
    
            // Check if name has been entered
            if (!$_POST['name']) {
                $errName = 'Please enter your name';
            }
    
            // Check if email has been entered and is valid
            if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
                $errEmail = 'Please enter a valid email address';
            }
    
            //Check if message has been entered
            if (!$_POST['message']) {
                $errMessage = 'Please enter your message';
            }
            //Check if simple anti-bot test is correct
            if ($human !== 5) {
                $errHuman = 'Your anti-spam is incorrect';
            }
    
    // If there are no errors, send the email
    if (!$errName && !$errEmail && !$errMessage && !$errHuman) {
        if (mail ($to, $subject, $body, $from)) {
            $result='<div class="alert alert-success">Thank You! I will be in touch</div>';
        } else {
            $result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later</div>';
        }
    }
        }
    ?>
    

    我从中得到的结果是这样的:

    Screenshot

    在我看来,HTML文件并不执行PHP代码,只是显示原始代码。我尝试将HTML保存为.html和.php,两者都给出了不同但错误的结果。

4 个答案:

答案 0 :(得分:0)

只是:

<?php if (isset($errName)) { echo "<p class='text-danger'>$errName</p>"; } ?>

适用于所有领域。

征求意见:

 // Create this variable for your error detection
 $hasError = FALSE;

 // Check if name has been entered
 if (!$_POST['name']) {
      $errName = 'Please enter your name';
      $hasError = TRUE;
 }

 // Check if email has been entered and is valid
if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
    $errEmail = 'Please enter a valid email address';
    $hasError = TRUE;
}

//Check if message has been entered
if (!$_POST['message']) {
    $errMessage = 'Please enter your message';
    $hasError = TRUE;
}

//Check if simple anti-bot test is correct
if ($human !== 5) {
   $errHuman = 'Your anti-spam is incorrect';
   $hasError = TRUE;
}

// If there are no errors, send the email
if ($hasError == FALSE) {
   ....

答案 1 :(得分:0)

正如文森特刚刚提到的,这应该是解决问题的解决方案,但它也可以有一些解释。

PHP只能回显现有变量,所以为了回应它们,它们需要存在,否则会引发错误。

基本上isset()函数检查变量是否存在。 isset()会返回PHP docs中提到的bool(true为false)。

您可以查看此示例以获取更多详细信息:W3School

答案 2 :(得分:0)

看起来很好,因为在使用之前没有使用isset()验证所有变量,如

if(isset($varialbe))
{
   // ... use $varialbe here ...
}

你可以在三元运算符(条件)的帮助下使用它吗?true part:false part;像

$othervariable = (isset($varialbe))?$varialbe:'';

echo ((isset($varialbe))?$varialbe:'');

答案 3 :(得分:0)

首先,将error_reporting置于顶部。

error_reporting(0);

然后再次提交表格。确保表单文件必须以.php格式保存。

我可以看到你的文件代码应该是这样的:

<?php
    error_reporting(0);
    if (isset($_POST["submit"])) {
        $name = $_POST['name'];
        $email = $_POST['email'];
        $message = $_POST['message'];
        $human = intval($_POST['human']);
        $from = 'Demo Contact Form'; 
        $to = 'test@testmail.com'; 
        $subject = 'Message from Contact Demo ';

        $body = "From: $name\n E-Mail: $email\n Message:\n $message";

        // Check if name has been entered
        if (!$_POST['name']) {
            $errName = 'Please enter your name';
        }

        // Check if email has been entered and is valid
        if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
            $errEmail = 'Please enter a valid email address';
        }

        //Check if message has been entered
        if (!$_POST['message']) {
            $errMessage = 'Please enter your message';
        }
        //Check if simple anti-bot test is correct
        if ($human !== 5) {
            $errHuman = 'Your anti-spam is incorrect';
        }

// If there are no errors, send the email
if (!$errName && !$errEmail && !$errMessage && !$errHuman) {
    if (mail ($to, $subject, $body, $from)) {
        $result='<div class="alert alert-success">Thank You! I will be in touch</div>';
    } else {
        $result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later</div>';
    }
}
    }
?>
<form action="yourfilename.php" role="form" method="post" class="">
    <div class="form-group">
        <label for="name" class="col-sm-2 control-label sr-only">Naam</label>
        <input type="text" class="form-control form-control-sm form" id="name" name="name" placeholder="Name" value="<?php echo htmlspecialchars($_POST['name']); ?>">
        <?php echo "<p class='text-danger'>$errName</p>";?>
    </div>
    <div class="form-group">
        <label for="email" class="col-sm-2 control-label sr-only">Email</label>
        <input type="email" class="form-control form-control-sm form" id="email" name="email" placeholder="your.name@example.com" value="<?php echo htmlspecialchars($_POST['email']); ?>">
        <?php echo "<p class='text-danger'>$errEmail</p>";?>
    </div>
    <div class="form-group">
        <label for="message" class="col-sm-2 control-label sr-only">Message</label>
        <textarea class="form-control form-control-sm form" rows="4" name="message" placeholder="My Message..."><?php echo htmlspecialchars($_POST['message']);?></textarea>
        <?php echo "<p class='text-danger'>$errMessage</p>";?>
    </div>
    <div class="form-group">
        <label for="human" class="col-sm-2 control-label sr-only">2 + 3 = ?</label>
        <input type="text" class="form-control form-control-sm form" id="human" name="human" placeholder="2 + 3 = ?">
        <?php echo "<p class='text-danger'>$errHuman</p>";?>
    </div>
    <div class="form-group">
        <input id="submit" name="submit" type="submit" value="Send" class="btn btn-primary btn-sm">
        <input type="reset" value="Clear" class="btn btn-primary btn-sm">
    </div>
    <div class="form-group">
        <?php echo $result; ?>
    </div>
</form>