我刚开始学习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 *: </label>
<input type="text" name="name" id="name" placeholder="Enter your Name " class="form-control">
</div>
<div class="form-group">
<label for="email">Email *:</label>
<input type="email" name="email" id="email" placeholder="Enter your Email " class="form-control">
</div>
<div class="form-group">
<label for="message">Message *:</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>
另外,如果你帮我生成一个随机数并在我的代码中使用它会很棒。我在我的代码中尝试过,请告诉我它是否正确。
谢谢