我制作了一个网络表单,用于发送包含表单信息的电子邮件。我有一些我无法解决的问题,我希望有人可以提供帮助。
HTML:
<form method="post" action="mail.php">
<div class="row uniform">
<div class="6u 12u$(xsmall)">
<label for="name">Full Name</label>
<input name="name" type="text" class="required" id="name" value="" />
</div>
<div class="6u$ 12u$(xsmall)">
<label for="email">Email Address</label>
<input name="email" type="email" class="required" id="email" value="" />
</div>
<div class="6u 12u$(xsmall)">
<label for="date">Date of Birth</label>
<input name="date" type="text" class="required" id="date" value="" />
</div>
<div class="6u$ 12u$(xsmall)">
<label for="age">Age as of July 1, 2016</label>
<input name="age" type="text" class="required" id="age" value="" />
</div>
<div class="6u 12u$(xsmall)">
<label for="phone">Phone Number</label>
<input name="phone" type="tel" class="required" id="phone" value="" />
</div>
<div class="12u$">
<label for="club">Current Club Membership</label>
<div class="select-wrapper">
<select name="club" class="required" id="club">
<option value="">-</option>
<option value="Colonial Road Runners">Colonial Road Runners</option>
<option value="Peninsula Track Club">Peninsula Track Club</option>
<option value="Tidewater Striders">Tidewater Striders</option>
</select>
</div>
</div>
<div class="12u$">
<label for="message">Message</label>
<textarea name="message" id="message" rows="6"></textarea>
</div>
<div class="12u$">
<ul class="actions">
<li><input type="submit" name="submit" value="Send Message" class="special" /></li>
<li><input type="reset" value="Reset" /></li>
</ul>
</div>
</div>
</form>
php:
<?php
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$date = $_POST['date'];
$age = $_POST['age'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$from = 'From: hrsupergp@gmail.com';
$to = '6linewrasse@gmail.com';
$subject = 'New Registration';
$body.='email: '. $_POST['email']."\n";
$body.='date: '. $_POST['date']."\n";
$body.='age: '. $_POST['age']."\n";
$body.='phone: '. $_POST['phone']."\n";
$body.='club: '. $_POST['club']."\n";
$body = "From: $name
E-Mail: $email
Date: $date
Age 7/2016: $age
Phone: $phone
Club: $club
Message: $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';
}
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>';
}
}
?>
答案 0 :(得分:3)
我也可以在这里发表答案。
您的上次$body
已损坏
$body = "From: $name
并且缺少连接.
将其更改为:
$body .= "From: $name...
对于重定向,它就像做标题一样简单。
header('Location: http://www.example.com/');
exit;
但是,请确保您的PHP没有任何内容,因为您可能在标题之前输出。
如果您的HTML表单高于PHP,请将其放在它下面。
如果您收到标题发送通知,请咨询:
旁注:
最好为所有内容使用条件empty()
以避免空白数据。
<强>脚注:强>
标题,如果您要使用它,应该放在您目前拥有$result='<div class="alert alert-success">Thank You! I will be in touch</div>';
的位置。如果您在某个地方没有向我们展示过$result
变量,请不要使用“with”,但替换。
将error reporting添加到文件的顶部,这有助于查找错误。
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// rest of your code
旁注:只应在暂存时进行显示错误,而不是生产。
答案 1 :(得分:0)
您需要在select标记onchange="document.getElementById('text_content').value=this.options[this.selectedIndex].text"
此输入用于存储选项
中的值<input type="hidden" name="test_text" id="text_content" value="" />
get the text of the selected option using php
要重定向,
header("Location: thankyou.html");
die();
在if语句之后返回的字符串将与您联系。 How to make a redirect in PHP?
您可以通过URL将字符串传递到另一个页面。只需在新页面上创建<div class="thankyou">
,网址就像thankyou.html?thankyou=thank%20you%20very%20much
。所以你的标题看起来像这样:
header("Location: thankyou.html?thankyou=thank%20you%20very%20much");
die();