duanmaifu3428 2015-11-07 05:31
浏览 42
已采纳

如何使用PHP使表单发送电子邮件?

I've been learning PHP for about two days now. So I'm really new to PHP. I have tried my best to understand but no matter what I write, my code never works. I got it to send an email successfully one time. But I'm not able to get it to send the contents in my form.

Here is my HTML form.

    <!-- 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 -->

Can someone please help me with a php code that would work for this form? I have found a couple template forms on the internet and I've spent hours trying to modify them to work with my form. But no luck.

And I can get the PHP and form to work together and make it send me an email from my server, so there's no problem with the server. An email will send. I just can't get the PHP code to send the contents of the form. It sends the email, but with no form details. Understand?

I really, really need help with this. Can you guys help me create a PHP code that will work with my HTML form I posted in this thread? I really need to get past this step in this project.

Thank you!

UPDATE!

Here is PHP template I found on the internet, but I don't know how to really modify it properly.... please help?

<?php 
$errors = '';
$myemail = 'example@example.com';//<-----Put Your email address here.
if(empty($_POST['firstname'])  || 
   empty($_POST['lastname']) || 
   empty($_POST['dob']))
{
    $errors .= "
 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 .= "
 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:
 Name: $name 
 Email: $email_address 
 Message 
 $message"; 

    $headers = "From: $myemail
"; 
    $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>
  • 写回答

2条回答 默认 最新

  • duanjue6584 2015-11-07 05:56
    关注

    You have a several code errors...

    1) Remove the <label> tags from your titles for <input type="text">... they are not needed.
    2) Make sure your form field name matches your email code $_POST['something'] for example
    You have <input type="text" name="memberFirstName" id="memberFirstName" /> in your form... BUT you have $_POST['firstname'] in your email code. Change $_POST to memberFirstName OR change your <input name and id to firstname

    The same changes need to be made to all elements so they match precisely.

    AND remember... PHP is case sensitive.

    Happy Coding !

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化