我已经学习PHP大约两天了。所以我是PHP新手。我尽力去理解,但无论我写什么,我的代码都无法运作。我得到它成功发送一封电子邮件。但是我无法让它以我的形式发送内容。
这是我的HTML表单。
<!-- FORM BEGINS -->
<form action="contact-form-handler.php" method="post">
<!-- Full Name -->
<div class="formWrapper ">
<label name="firstname" for="firstName"><span>*</span>First Name</label>
<input type="text" name="memberFirstName" id="memberFirstName" onkeyup="activateButton()" class="inputText" tabindex="1" maxlength="256" onkeydown="return checkAlphaNumericText(event,this);" required="required"/>
</div>
<p class="fieldsetDivider"></p>
<div class="formWrapper">
<label for="lastName"><span>*</span>Last Name</label>
<input type="text" name="memberLastName" id="memberLastName" onkeyup="activateButton()" class="inputText" tabindex="2" maxlength="256" onkeydown="return checkAlphaNumericText(event,this);" required="required"/>
</div>
<p class="fieldsetDivider"></p>
<!-- Date of Birth-->
<p class="fieldsetDivider"></p>
<div class="formWrapper smallInput">
<label for="firstInput"><span>*</span>Date of Birth
<div class="labelClarify">(MM/DD/YYYY)</div>
</label>
<div class="inlineInput">
<input type="text" name="month" id="month" maxlength="2" onkeyup="activateButton();autotab(this, 'day')" onkeypress="return isNumberKey(event)" class="inputText" placeholder="MM" required="required"/>
<span>/</span>
</div>
<div class="inlineInput">
<input type="text" name="day" id="day" maxlength="2" onkeyup="activateButton();autotab(this, 'year')" onkeypress="return isNumberKey(event)" class="inputText" placeholder="DD" required="required"/>
<span>/</span>
</div>
<div class="inlineInput">
<input type="text" name="year" id="year" maxlength="4" onkeyup="activateButton()" onkeypress="return isNumberKey(event)" class="inputText" placeholder="YYYY" required="required"/>
</div>
<a rel="Please enter your date of birth" class="formHelp tooltip-link" href="#">
<img src="../../images/site3/common/form_icon_help.png">
</a>
</div>
<p class="fieldsetDivider"></p>
<!-- Member ID -->
<div class="formWrapper">
<label for="memberID"><span>*</span>Member ID</label>
<div class="inlineInput">
<input type="text" name="memberID" id="memberID" class="inputText" onkeyup="activateButton()" tabindex="3" maxlength="9" onkeypress="return isNumberKey(event)" required="required"/>
</div>
<a rel="Please enter your member ID" class="formHelp tooltip-link" href="#">
<img src="../../images/site3/common/form_icon_help.png">
</a>
</div>
<p class="fieldsetDivider"></p>
<!-- Zip Code -->
<div class="formWrapper">
<label for="zipCode"><span class="requiredStar">*</span>Zip Code</label>
<div class="inlineInput">
<input type="text" name="memberZipCode" id="memberZipCode" onkeyup="activateButton()" tabindex="4" class="inputText" maxlength="5" onkeypress="return isNumberKey(event)"/>
</div>
</div>
<p class="fieldsetDivider"></p>
<!-- Button -->
<div class="button">
<button class="greenBtnLarge" type="submit" style="text-decoration:none;"><span>Next<span></button>
</div>
</form>
<!-- FORM ENDS -->
有人可以帮助我使用适用于此表单的PHP代码吗?我在互联网上找到了几个模板表单,我花了几个小时试图修改它们以使用我的表单。但没有运气。
我可以让PHP和表单一起工作并让它从我的服务器发送给我一封电子邮件,因此服务器没有问题。电子邮件将发送。我只是无法获取PHP代码来发送表单的内容。它发送电子邮件,但没有表格细节。理解?
我真的非常需要帮助。你能帮助我创建一个PHP代码,它可以使用我在这个帖子中发布的HTML表单吗?我真的需要在这个项目中超越这一步。
谢谢!
UPDATE!
这是我在互联网上找到的PHP模板,但我不知道如何正确地修改它....请帮忙?
<?php
$errors = '';
$myemail = 'example@example.com';//<-----Put Your email address here.
if(empty($_POST['firstname']) ||
empty($_POST['lastname']) ||
empty($_POST['dob']))
{
$errors .= "\n Error: all fields are required";
}
$name = $_POST['firstname'];
$email_address = $_POST['lastname'];
$message = $_POST['dob'];
if (!preg_match(
"/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i",
$email_address))
{
$errors .= "\n Error: Invalid email address";
}
if( empty($errors))
{
$to = $myemail;
$email_subject = "Contact form submission: $name";
$email_body = "You have received a new message. ".
" Here are the details:\n Name: $name \n Email: $email_address \n Message \n $message";
$headers = "From: $myemail\n";
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
//redirect to the 'thank you' page
header('Location: contact-form-thank-you.html');
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Contact form handler</title>
</head>
<body>
<!-- This page is displayed only if there is some error -->
<?php
echo nl2br($errors);
?>
</body>
</html>
答案 0 :(得分:1)
您有几个代码错误......
1)从<label>
的标题中删除<input type="text">
标签......不需要它们
2)确保您的表单字段name
与您的电子邮件地址$_POST['something']
匹配,例如
您的表单中有<input type="text" name="memberFirstName" id="memberFirstName" />
...但是
您的电子邮件地址中包含$_POST['firstname']
。将$_POST
更改为memberFirstName
或将您的<input
名称和ID更改为firstname
需要对所有元素进行相同的更改,以便它们精确匹配。
请记住...... PHP区分大小写。
快乐的编码!
答案 1 :(得分:0)
HTML表单字段与php电子邮件处理程序
中的字段不同实施例
输入名称应与处理程序应获得的名称相同 由于
<input type="text" name="memberFirstName" required="required"/>
$ _POST的索引应该相同memberFirstName
$_POST['memberFirstName']
这应该适用于所有字段
似乎HTML表单根本与php处理程序无关。
最好使用此表单进行测试
<form action="contact-form-handler.php" method="POST">
First Name: <input name="firstname" type="text"><br>
Last Name:<input name="lastname" type="text"><br>
DOB: <input name="dob" type="text">
<button type="submit">Send</button>
</form>
注意:强>
如果您使用的是wamp或xampp本地服务器,则需要检查电子邮件服务是否配置正确,否则将不会发送电子邮件 检查以供参考: How to configure XAMPP to send mail from localhost?