简体   繁体   English

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

[英]HTML form showing code instead of PHP output

I've been trying to implement this form into my HTML (Bootstrap 4 alpha) website, but I can't get it working properly. 我一直试图在我的HTML(Bootstrap 4 Alpha)网站中实现此表单,但无法使其正常运行。

Let's start with the goals: 让我们从目标开始:

  1. A working form that checks if valid input is given for all fields before it sends an email. 一种工作表格,用于在发送电子邮件之前检查是否为所有字段提供了有效输入。
  2. If the user didn't enter a valid input for one or more of the fields, an error message should appear on the page itself (so no redirect to an error page). 如果用户没有为一个或多个字段输入有效输入,则错误消息应出现在页面本身上(因此请不要重定向到错误页面)。
  3. preferably, when the user made a mistake with one of the input fields and the error shows up, the previously entered data shall remain in the fields. 优选地,当用户对输入字段之一犯了错误并且错误出现时,先前输入的数据应保留在这些字段中。

I found some useful help somewhere on the internet, a PHP and HTML of which I could use the stuff I needed. 我在互联网上的某个地方找到了一些有用的帮助,可以使用所需的PHP和HTML。 However, this form doesn't work, even when I upload the original code without changing anything. 但是,即使我在不​​做任何更改的情况下上传原始代码,也无法使用此表单。

I thought PHP might not be supported by the server I was uploading it to, but it is and I even got a form working that would just take the input and send it to the receiver with no questions asked. 我以为我上传的服务器可能不支持PHP,但是确实如此,我什至得到了一个可以将输入内容毫无疑问地发送到接收方的表单。

What I didn't get to work though was to echo an output of my PHP file in my HTML. 但是,我没有工作的是在HTML中回显PHP文件的输出。 What am I doing wrong? 我究竟做错了什么?

HTML: 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:

<?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>';
    }
}
    }
?>

The results I get from this are like this: 我从中得到的结果是这样的:

屏幕截图

Seems to me that the HTML file doesn't execute the PHP code, instead just displays raw code. 在我看来,HTML文件不会执行PHP代码,而是仅显示原始代码。 I tried both saving the HTML as .html and as .php, both gave different but false results. 我尝试将HTML保存为.html和.php,结果都不同,但结果都是错误的。

Just : 只是:

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

For all your fields. 对于您所有的领域。

For your comment : 对于您的评论:

 // 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) {
   ....

As Vincent just mentioned, that should be the solution to solve your problem, but it could have a little explanation as well. 正如文森特(Vincent)刚才提到的那样,这应该是解决您的问题的解决方案,但也可能会有一些解释。

PHP only can echo existing variables, so in order to echo them they need to exist, otherwise it will throw an error. PHP仅可以回显现有变量,因此为了回显它们,它们必须存在,否则将引发错误。

So basically the isset() function checks if the variable exists or not. 因此,基本上, isset()函数检查变量是否存在。 isset() returns a bool(true of false) as mentioned in the PHP docs . isset()返回bool(false的true),如PHP docs中所述

You can take a look at this example for more details: W3School 您可以看一下此示例以了解更多详细信息: W3School

looks its all because you have not validated all your variables using isset() before using it like 看起来全部是因为您没有像使用它之前使用isset()验证所有变量

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

you can use same with help of ternary operator (condition)?true part:false part; 您可以在三元运算符(条件)的帮助下使用它吗? like 喜欢

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

or 要么

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

First of all, turn error_reporting to off by putting this in the top. 首先,将error_reporting置于顶部以将其关闭。

error_reporting(0);

Then submit the form again. 然后再次提交表格。 Make sure form file must saved in .php format. 确保表单文件必须以.php格式保存。

And I can see your file code should be like this: 我可以看到您的文件代码应如下所示:

<?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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM