Php HTML5 站点的联系表单部分无法正常工作
Php Contact form portion of HTML5 site not working
我目前有我的网站 www.vivascoaching.com,但我似乎无法获得页面的联系表格部分,以便在填写完我的表格后通过电子邮件回复我。目前我有一个 index.html 文件和一个单独的 php 文件,名为 callback.php 我是 php 的新手,所以我不确定这是否是正确的方法。
这是 index.html 文件:
<!DOCTYPE HTML>
<html>
<head>
<title>Vivas Coaching-Main</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="../style/main.css">
<link rel="stylesheet" href="../style/normalize.css">
</head>
<body>
<header>
<img src="Images/logo.png" alt="logo" width="465px" height="135px" align="middle">
</header>
<div class="left_column">
<nav id="secondarylinks">
<ul>
<li><a href="register.html" title="Register">Register</a></li>
<li><a href="dates.html" title="Dates">Dates</a></li>
<li><a href="pricing.html" title="Pricing">Pricing</a></li>
<li><a href="forms.html" title="Forms">Forms</a></li>
</ul>
</nav>
</div>
<div class="middle_column">
<nav id="mainlinks">
<ul>
<li><a href="index.html" title="Main">Main</a></li>
<li><a href="classes.html" title="Classes">Classes</a></li>
<li><a href="teambuilding.html" title="Team Building">Team Building</a></li>
</ul>
</nav>
<img src="Images/SAT summer flyer.jpg" alt="SAT summer flyer" width="800px" height="800px">
<footer>
<p>©VivasCoaching 2016</p>
</footer>
</div>
<div class="right_column">
<h2>Contact Us</h2>
<p>(646)316-8481/<br>(403)718-0159</p>
<p>Please fill out the information below and we will get back to you as soon as possible!<p>
<form method="post" action="callback.php">
<label for="firstname">First Name: </label>
<input type="text" name="firstname"/>
<label for="lastname">Last Name: </label>
<input type="text" name="lastname"/>
<label for="email">Email: <span class="required"></label>
<input type="text" name="email"/>
<label>*What is 2+2? (Anti-spam)</label>
<input name="human" placeholder="Type Here">
<label for="message"> Message: <span class="required"></label>
<textarea id="message" name="message" cols="25" rows="10" placeholder="Type your message here!"></textarea>
<input type="submit" id="submit"/>
</form>
</div>
</body>
</html>
这是 callback.php:
<?php
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
$message = $_POST['message'];
$to = 'vivascoaching@gmail.com';
$subject = 'inquiry';
$human = $_POST['human'];
$submit = $_POST['submit'];
$body ="From: $firstname\n $lastname\n Email: $email\n Message:\n $message";
if ($_POST['submit'] && $human == '4') {
if (mail ($to, $subject, $body)) {
echo '<p>Your message has been sent!</p>';
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
} else if ($_POST['submit'] && $human != '4') {
echo '<p>You answered the anti-spam question incorrectly!</p>';
}
?>
已编辑
我已经编辑了你的代码伙计。而且输入不正确。我的意思是你可以使用
<input type="email" name="email" placeholder="email" />
用于电子邮件。 (该输入需要一个@domain。)
此外,您可以使用必需的。类似于 <input type="text" name="test" required />
.
我已经编辑了你的回传。用这个。
检查编辑 3
我摆脱了 isset($_POST['submit']),那真是令人头疼!
编辑 2:
切勿对 php 中的数字使用引号('
或 "
)。那是不需要的。
编辑 3(希望是最后一个。)
如果您想在索引中显示消息,我使用 headers 通过 GET 请求重定向。
例如:index.php
<!doctype html>
<html>
<head>
</head>
<body>
<?php
error_reporting(0) // prevent undefined index.
if(isset($_GET['success']) && $_GET['success']) {
echo "<p>Your message has been sent!</p>";
} elseif(isset($_GET['error']) && $_GET['error'] && isset($_GET['message'])) {
echo $_GET['message']; // Print message like ?error=true&message=test.
// echo '<p>'.$_GET['message'].'</p>'; if you don't know how to add html before it.
}
?>
</body>
</html>
(索引需要为 .php)!
callback.php 将是:
<?php
if(isset($_POST['human']) && isset($_POST['email'])) {
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
$message = $_POST['message'];
$to = 'vivascoaching@gmail.com';
$subject = 'inquiry';
$human = $_POST['human'];
$body ="From: $firstname\n $lastname\n Email: $email\n Message:\n $message";
if ($human == 4) {
if (mail ($to, $subject, $body)) {
header('location:index.php?success=true');
} else {
header('location:index.php?error=true&message=Something went wrong, go back and try again!');
}
} else if ($human != 4) {
header('location:index.php?error=true&message=You answered the anti-spam question incorrectly!');
}
} else { header('location:index.php?error=true&message=Form is incomplete.'); }
?>
我目前有我的网站 www.vivascoaching.com,但我似乎无法获得页面的联系表格部分,以便在填写完我的表格后通过电子邮件回复我。目前我有一个 index.html 文件和一个单独的 php 文件,名为 callback.php 我是 php 的新手,所以我不确定这是否是正确的方法。
这是 index.html 文件:
<!DOCTYPE HTML>
<html>
<head>
<title>Vivas Coaching-Main</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="../style/main.css">
<link rel="stylesheet" href="../style/normalize.css">
</head>
<body>
<header>
<img src="Images/logo.png" alt="logo" width="465px" height="135px" align="middle">
</header>
<div class="left_column">
<nav id="secondarylinks">
<ul>
<li><a href="register.html" title="Register">Register</a></li>
<li><a href="dates.html" title="Dates">Dates</a></li>
<li><a href="pricing.html" title="Pricing">Pricing</a></li>
<li><a href="forms.html" title="Forms">Forms</a></li>
</ul>
</nav>
</div>
<div class="middle_column">
<nav id="mainlinks">
<ul>
<li><a href="index.html" title="Main">Main</a></li>
<li><a href="classes.html" title="Classes">Classes</a></li>
<li><a href="teambuilding.html" title="Team Building">Team Building</a></li>
</ul>
</nav>
<img src="Images/SAT summer flyer.jpg" alt="SAT summer flyer" width="800px" height="800px">
<footer>
<p>©VivasCoaching 2016</p>
</footer>
</div>
<div class="right_column">
<h2>Contact Us</h2>
<p>(646)316-8481/<br>(403)718-0159</p>
<p>Please fill out the information below and we will get back to you as soon as possible!<p>
<form method="post" action="callback.php">
<label for="firstname">First Name: </label>
<input type="text" name="firstname"/>
<label for="lastname">Last Name: </label>
<input type="text" name="lastname"/>
<label for="email">Email: <span class="required"></label>
<input type="text" name="email"/>
<label>*What is 2+2? (Anti-spam)</label>
<input name="human" placeholder="Type Here">
<label for="message"> Message: <span class="required"></label>
<textarea id="message" name="message" cols="25" rows="10" placeholder="Type your message here!"></textarea>
<input type="submit" id="submit"/>
</form>
</div>
</body>
</html>
这是 callback.php:
<?php
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
$message = $_POST['message'];
$to = 'vivascoaching@gmail.com';
$subject = 'inquiry';
$human = $_POST['human'];
$submit = $_POST['submit'];
$body ="From: $firstname\n $lastname\n Email: $email\n Message:\n $message";
if ($_POST['submit'] && $human == '4') {
if (mail ($to, $subject, $body)) {
echo '<p>Your message has been sent!</p>';
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
} else if ($_POST['submit'] && $human != '4') {
echo '<p>You answered the anti-spam question incorrectly!</p>';
}
?>
已编辑
我已经编辑了你的代码伙计。而且输入不正确。我的意思是你可以使用
<input type="email" name="email" placeholder="email" />
用于电子邮件。 (该输入需要一个@domain。)
此外,您可以使用必需的。类似于 <input type="text" name="test" required />
.
我已经编辑了你的回传。用这个。
检查编辑 3
我摆脱了 isset($_POST['submit']),那真是令人头疼!
编辑 2:
切勿对 php 中的数字使用引号('
或 "
)。那是不需要的。
编辑 3(希望是最后一个。)
如果您想在索引中显示消息,我使用 headers 通过 GET 请求重定向。
例如:index.php
<!doctype html>
<html>
<head>
</head>
<body>
<?php
error_reporting(0) // prevent undefined index.
if(isset($_GET['success']) && $_GET['success']) {
echo "<p>Your message has been sent!</p>";
} elseif(isset($_GET['error']) && $_GET['error'] && isset($_GET['message'])) {
echo $_GET['message']; // Print message like ?error=true&message=test.
// echo '<p>'.$_GET['message'].'</p>'; if you don't know how to add html before it.
}
?>
</body>
</html>
(索引需要为 .php)!
callback.php 将是:
<?php
if(isset($_POST['human']) && isset($_POST['email'])) {
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
$message = $_POST['message'];
$to = 'vivascoaching@gmail.com';
$subject = 'inquiry';
$human = $_POST['human'];
$body ="From: $firstname\n $lastname\n Email: $email\n Message:\n $message";
if ($human == 4) {
if (mail ($to, $subject, $body)) {
header('location:index.php?success=true');
} else {
header('location:index.php?error=true&message=Something went wrong, go back and try again!');
}
} else if ($human != 4) {
header('location:index.php?error=true&message=You answered the anti-spam question incorrectly!');
}
} else { header('location:index.php?error=true&message=Form is incomplete.'); }
?>