我是PHP的初学者,并尝试设置一个联系表单,在使用正则表达式验证后,使用PHP脚本将输入的信息发送到电子邮件地址,然后打开“谢谢”页面。我一直在尝试按照以下页面上的教程进行操作:http://myphpform.com/php-form-tutorial.php
我使用统一服务器设置了一个测试服务器,并使用邮件服务器正在运行的mailtest.php文件建立。但是尽量我可能无法使用我的PHP脚本来处理我的表单,所以非常感谢任何人都可以提供的建议。
HTML - 页面位于:http://www.plymouthparentpartnership.org.uk/index.php?p=32_3
<span class="title">Parent Request for Involvement in Parenting Programmes</span>
<p>If you are a <span style="font-weight: bold;">Parent</span>, please complete this form to request a Parenting Programme.</p>
<p>If you are a <span style="font-weight: bold;">Professional</span>, please use the <a href="http://www.plymouthparentpartnership.org.uk/index.php?p=32_4" title="Professional Form to request Parenting Programme">Professional Form</a> to request a Parenting Programme.</p>
<div style="WIDTH: 75%" class="box-round-corner">
<form action="parent-p-programme.php" method="post">
<div>
<div>
<div>
<table width="100%" cellspacing="0" cellpadding="2" border="0">
<tbody>
<tr>
<td colspan="2"> <span class="error">* required field.</span></td>
</tr>
<tr>
<td>Name:</td>
<td>
<input type="text" name="name" size="35" /> <span class="error"> *</span></td>
</tr>
<tr>
<td style="VERTICAL-ALIGN: top">Telephone Number or<br />
Mobile number if main number:</td>
<td>
<input type="text" name="tel" size="35" /> <span class="error"> *</span></td>
</tr>
<tr>
<td>Mobile Number:</td>
<td>
<input type="text" name="mobile" size="35" /></td>
</tr>
<tr>
<td>Email Address:</td>
<td>
<input type="email" name="email" size="35" /></td>
</tr>
<tr>
<td>Course Required:</td>
<td>
<select name="course">
<option selected="selected">Please select course</option>
<option>Incredible Years</option>
<option>Strengthening Families 10-14 years</option>
</select> <span class="error"> *</span></td>
</tr>
<tr>
<td style="VERTICAL-ALIGN: top">Date of Birth of<br />
Child / Young Person:</td>
<td>
<input type="text" name="dob" size="35" /> <span class="error"> *</span></td>
</tr>
<tr>
<td style="VERTICAL-ALIGN: top">Additional Information:</td>
<td>
<textarea name="info" rows="5" cols="32"></textarea></td>
</tr>
</tbody>
</table>
<p>We aim to respond to your enquiry within 2 working days.</p>
<p>
<input type="submit" value="Send Request" />
<input type="reset" value="Clear Form" /></p></div> </div> </div>
</form></div>
PHP脚本
<?php
/* Parent request for Parenting Programme */
/* Set e-mail recipient */
$myemail = "myemailaddress@gmail.com"; // dummy email
/* Check all form inputs using check_input function */
$name = check_input($_POST['name'], "Enter your name"); // required
$tel = check_input($_POST['tel'], "Enter your telephone number or mobile number if this is your main number"); // required
$mobile = check_input($_POST['mobile']);
$email = check_input($_POST['email']);
$course = check_input($_POST['course'], "Select course required"); // required
$dob = check_input($_POST['dob'], "Enter young persons date of birth"); // required
$info = check_input($_POST['info']);
/* If tel no is not valid show error message */
if (!preg_match("/([0-9 ]{6})?([0-9]{6})/", $tel))
{
$tel = '';
}
/* Validate mobile no (optional) */
if (!preg_match("/([0-9 ]{6})?([0-9]{6})/", $mobile))
{
$mobile = '';
}
/* If e-mail is not valid show error message (optional) */
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
{
$email = '';
}
/* Email Message */
$message = "Request for $course
Name: $name
Tel No: $tel
Mobile: $mobile
Email: $email
Course: $course
Young Person's Date of Birth: $dob
Additional information:
$info
End of message
";
/* Send message using mail () function */
mail($myemail, $message);
/* Redirect to thank you page */
header('Location: 32_5.html');
exit();
?>
<?php
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}
function show_error($myError)
{
?>
<html>
<body>
<b>Please correct the following error:</b><br />
<?php echo $myError; ?>
</body>
</html>
<?php
exit();
}
?>
在完成字段后弹出提交页面的“谢谢”,它似乎可以识别字段未填写的位置,但验证似乎也没有正常工作。
表单提交后感谢页面
<span class="title">Parenting Programme Request</span>
<p>Your request for a Parenting Programme was sent.</p>
<p>We aim to respond to your request within 2 working days.</p>
当我在本地测试时,PHP脚本没有发送任何电子邮件。整晚都看着它,但看不出我做错了什么。所以,请提前感谢您提供任何帮助或建议。
答案 0 :(得分:0)