PHP代码按原样打印

时间:2017-08-11 09:14:06

标签: php

我刚开始学习PHP,并尝试使用PHP创建响应式联系表单。 但问题是我的PHP代码按原样打印在网页上,而不是我需要的格式。

这是我的代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Contact Us</title>
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" type="text/css" href="styles.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/start/jquery-ui.css">
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-
ui.min.js"></script>
</head>
<body>
<!-- <img src="call.jpg" id="background"> -->
    <div id="container-fluid">  
        <div class="row">
            <div class="col-sm-offset-1 col-sm-10 contactform">
                <h1>Contact Us:</h1>
                <?php
                    $name = $_REQUEST["name"];
                    $email = $_REQUEST["email"];
                    $message = $_REQUEST["message"];
                    $submit = $_REQUEST["submit"];
                    $to = "abc1996@xyz.com";
                    $var = rand(1,1000);
                    $subject = 'Complaint Registered. Complaint No.: '. echo "$var";
                    if($submit)
                    {
                        if(!$name)
                            $errors = $errors."<p><strong>Name missing!</strong></p>";
                        else
                            $name = filter_var($name,FILTER_SANITIZE_STRING);
                        if(!$email)
                            $errors = $errors."<p><strong>Email missing!</strong></p>";
                        else
                        {
                            $name = filter_var($email,FILTER_SANITIZE_STRING);
                            if(!filter_var($email,FILTER_VALIDATE_EMAIL))
                            {
                                $errors = $errors."Please give a valid Email-Address!";
                            }
                        }
                        if(!$message)
                        {
                            $errors = $errors."<p><strong>Message box can't be empty!</strong></p>";
                        }
                        else
                        {
                            $message = filter_var($message,FILTER_SANITIZE_STRING);
                        }
                        if($errors)
                        {
                            $finalmsg = "<div class='alert alert-danger'> ".$errors "</div>";
                        }
                        else
                        {
                            $date = date('d MM YY');
                            $content = "Hi $name. Thank you for your complaint. Your complaint has been registered on $date and your complaint number is: $var";
                            if(mail($to, $subject, $content))
                            {
                                $finalmsg = '<div class="alert alert-success">Your mail has been sent and we will REQUEST back to you asap</div>';
                            }
                            else
                            {
                                $finalmsg = '<div class="alert alert-warning">Error Sending Mail. Try again later!</div>';
                            }
                        }
                        echo $finalmsg;
                    }
                ?>
                <form action="" method="post">
                    <div class="form-group">
                        <label for="name">Name &#42: </label>
                        <input type="text" name="name" id="name" placeholder="Enter your Name " class="form-control">
                    </div>
                    <div class="form-group">
                        <label for="email">Email &#42:</label>
                        <input type="email" name="email" id="email" placeholder="Enter your Email " class="form-control">
                    </div>
                    <div class="form-group">
                        <label for="message">Message &#42:</label>
                        <textarea id="message" name="message" class="form-control" rows="5"></textarea>
                    </div>
                    <input type="submit" name="submit" class="btn btn-success btn-lg" value="Send Message" id="submit">
                </form>
            </div>
        </div>
    </div>
</body>

另外,如果你帮我生成一个随机数并在我的代码中使用它会很棒。我在我的代码中尝试过,请告诉我它是否正确。

谢谢

1 个答案:

答案 0 :(得分:0)

此网页是否由网络服务器提供,是否有.php扩展名?

PHP是一个预处理器,这意味着它需要由Web服务器运行。试试WAMP(Windows)或MAMP(对于Mac),将文件重命名为.php然后重试。