我似乎总是有问题和搞砸。大学没有教我们任何关于 PHP 的知识,我想用它来发送电子邮件。有人可以帮忙吗?我使用 JavaScript 进行验证,然后使用 PHP 发送表单。这就是我所拥有的。
PHP:
<?php
$to = 'jd20032007@hotmail.com';
$subject = 'Voice4Autism Inquiry';
$FirstName = $_POST['fname'];
$LastName = $_POST['lname'];
$eMail = $_POST['email'];
$message = <<<EMAIL
Hi!<br /><br/>
My name is $FirstName $LastName. I am interseted in your newsletter from Voice4Autism. Please add $eMail to your listserve.<br /><br />
Thank you,<br />
$FirstName $LastName
EMAIL;
$header = "From: $eMail\r\n";
$header = "Content-type: text/html\r\n";
mail($to, $subject, $message, $header);
?>
HTML/JavaScript
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Untitled Document</title>
<script>
function checkforblank() {
var errormessage = "";
if (document.getElementById('fname').value ==""){
errormessage += "enter your first name \n";
}
if (document.getElementById('lname').value ==""){
errormessage += "enter your last name \n";
}
if (document.getElementById('email').value ==""){
errormessage += "enter your email \n";
}
if (document.getElementById('confirmEmail').value ==""){
errormessage += "confirm your email \n";
}
if (errormessage != ""){
alert(errormessage);
return false;
} else return true;
}
function verifyEmail() {
var status = false;
var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
if (document.myForm.email.value.search(emailRegEx) == -1) {
alert("Please enter a valid email address.");
}
if (document.getElementById('email').value == document.getElementById('confirmEmail').value) {
alert("Thank you for your interest!");
status = true;
} else {
alert("Email addresses do not match. Please retype them to make sure they are the same.");
}
return status;
}
function confirmEmailAddresses(){
if (checkforblank() == true) {
if (verifyEmail() == true) {
document.getElementById("myForm").submit();
}
}
}
</script>
</head>
<body>
<div id="content">
<form name="myForm" action="#" method="get" enctype="application/x-www-form-urlencoded" onsubmit="">
<table width="377" height="96">
<tr>
<td style="text-align: right">First Name:</td>
<td><label for="FirstName"></label>
<input type="text" name="fname" id="fname"></td>
</tr>
<tr>
<td style="text-align: right">Last Name:</td>
<td><label for="LastName"></label>
<input type="text" name="lname" id="lname"></td>
</tr>
<tr>
<td style="text-align: right">E-mail:</td>
<td><input type="email" name="email" id="email"></td>
</tr>
<tr>
<td style="text-align: right">Confirm E-mail:</td>
<td><input type="email" name="confirmEmail" id="confirmEmail"></td>
</tr>
</table>
<input type="button" value="Send" onClick="confirmEmailAddresses()"><input type="reset" value="Reset Form">
</form>
</div>
</body>
</html>
JavaScript 和 HTML 确实有效!!!