我是一個初學PHP的人,試圖建立一個聯繫表格,使用正則表達式進行驗證後,使用PHP腳本將輸入的信息發送到一個電子郵件地址,然後打開一個「謝謝你「頁面。我一直在試圖按照下面的教程:http://myphpform.com/php-form-tutorial.php聯繫表通過電子郵件發送使用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 = "[email protected]"; // 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腳本不發送任何電子郵件後,當我在本地測試。花了整晚看着它,但只是看不到我做錯了什麼。因此,請提前感謝您提供任何幫助或建議。
您是否嘗試過打印出變量,以便您可以看到實際上正在傳遞電子郵件和其他條目? – Guardanis
郵件服務器是否工作?你能用'mail()'發送一個非常簡單的電子郵件嗎?另外,因爲你只是在學習這個功能,所以我建議使用其他的東西,比如PHPMailer。 – James
@sue D,正如你所描述的,你是PHP的新手,那麼爲什麼你使用這個所有功能的概念,這是一個簡單的形式,你也可以使它成爲一個簡單的,就像你的參考站點一樣, – SagarPPanchal