2017-04-08 78 views
0

我有一个自我张贴文件,有表单字段填写与php处理相同的文件。我的问题是,打开网页(网站)时,消息“消息已发送!”在表格可以填写信息之前立即显示。 PHP邮件()函数链接到我的电子邮件帐户,所以我收到表格数据电子邮件。但是没有数据发送,因为电子邮件是在填写表格之前发送的。我希望能够在发送电子邮件之前填写表格,以便表单发送实际信息。我研究过这个话题,并没有提出任何建议。任何帮助都是极好的!这里是我的代码...PHP邮件()发送邮件之前填写表格

<?php 
    foreach($_POST as $key => $value)  //This will loop through each name-value in the $_POST array 
    { 
     $tableBody .= "<tr>";    //formats beginning of the row 
     $tableBody .= "<td>$key</td>";  //dsiplay the name of the name-value pair from the form 
     $tableBody .= "<td>$value</td>"; //dispaly the value of the name-value pair from the form 
     $tableBody .= "</tr>";    //End this row 
    } 

    echo "<table border='1'>"; 
    echo "<tr><th>Field Name</th><th>Value of field</th></tr>"; 
    foreach($_POST as $key => $value) 
    { 
     echo '<tr class=colorRow>'; 
     echo '<td>',$key,'</td>'; 
     echo '<td>',$value,'</td>'; 
     echo "</tr>"; 
    } 
    echo "</table>"; 
    echo "<p>&nbsp;</p>"; 
?> 

<!DOCTYPE html> 

<head> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 

    <style> 

    body { 
     background-image: url("rbGrid.png"); 
     background-size: 150%; 
     background-repeat: no-repeat; 
     text-align: center; 
    } 

    div { 
     background-color: black; 
     opacity: 0.9; 
     color: white; 
     text-align: center; 
    } 

    h1 { 
     color: white; 
    } 

    h2 { 
     color: white; 
    } 

    #borderStyle { 
     border: double thick red; 
     border-radius: 45px; 
     width: 50%; 
     margin: 0 auto; 
    } 

    #hiddenStuff { 
     display: none; 
    } 

    textarea { 
     display: none; 
     margin: 0 auto; 
    } 

    #mailingInformation { 
     display: none; 
     margin: 0 auto; 
    } 

    table { 
     margin: 0 auto; 
     border: solid thick red; 
     border-radius: 20px; 
    } 

    th { 
     border: solid thick red; 
     border-radius: 45px; 
    } 

    tr { 
     color: white; 
     border: solid thin red; 
     border-radius: 45px; 
    } 

    td { 
     color: white; 
     border: solid thin red; 
     border-radius: 45px; 
    } 


</style> 

<script> 

    function showProductProblemComments() 
    { 
     document.getElementById("textarea").style.display = "block"; 
    } 

    function showMailingListForm() 
    { 
     document.getElementById("mailingInformation").style.display = "block"; 
    } 
</script> 
</head> 


<body> 

<?php 
    $toEmail = "[email protected]";  //CHANGE within the quotes. Place email address where you wish to send the form data. 
             //Use your DMACC email address for testing. 


    $subject = "WDV341 Email Example"; //CHANGE within the quotes. Place your own message. For the assignment use "WDV101 Email Example" 

    $fromEmail = "[email protected]";  //CHANGE within the quotes. Use your DMACC email address for testing OR 
             //use your domain email address if you have Heartland-Webhosting as your provider. 

// DO NOT CHANGE THE FOLLOWING LINES // 

    $emailBody = "Form Data\n\n ";   //stores the content of the email 
    foreach($_POST as $key => $value)  //Reads through all the name-value pairs. $key: field name $value: value from the form         
    { 
     $emailBody.= $key."=".$value."\n"; //Adds the name value pairs to the body of the email, each one on their own line 
    } 

    $headers = "From: $fromEmail" . "\r\n";    //Creates the From header with the appropriate address 

    if (mail($toEmail,$subject,$emailBody,$headers)) //puts pieces together and sends the email to your hosting account's smtp (email) server 
    { 
     echo("<p>Message successfully sent!</p>"); 
    } 
    else 
    { 
     echo("<p>Message delivery failed...</p>"); 
    } 

    /*$inName = $_POST["Name"];  
    $inEmail = $_POST["Email Address"]; 
    $inAddress = $_POST["address"]; 
    $inReason = $_POST["Reason"]; 
    $inComments = $_POST["comments"]; 
    $inMailBox = $_POST["Mailing List"]; 
    $inUseAddress = $_POST["checkForAddress"]; 
    $inFirstName = $_POST["mailingName"]; 
    $inLastName = $_POST["mailingLastName"]; 
    //$inMailingAdd $_POST["mailingAddress"]; 
    $inPhoneNumber = $_POST["phoneNumber"]; 
    $inMoreInfo = $_POST["More Info"];*/ 
?> 

<h1>WDV341 Intro PHP</h1> 
<h2>Programming Project - Contact Form</h2> 

<div> 
<form name="form1" method="POST" action="contactForm2.php"> 
    <p>&nbsp;</p> 
    <p> 
<div id = "borderStyle"> 
    <label>Your Name: 
     <input type="text" name="Name" id="textfield" required> 
    </p> 
<br><br> 
    <p>Your Email: 
    <input type="text" name="Email Address" id="textfield2" required> 
    </p> 
<br><br> 
    <p>Your Address: 
    <input type = "text" name = "address" id = "living"> 
    </p> 
<br><br> 
    <p>Reason for contact: 
     <select name="Reason" id="select2" onChange = "showProductProblemComments()" required> 
     <option value="default">Please Select a Reason</option> 
     <option value="product">Product Problem</option> 
     <option value="return">Return a Product</option> 
     <option value="billing">Billing Question</option> 
     <option value="technical">Report a Website Problem</option> 
     <option value="other">Other</option> 
     </select> 
    </p> 
<br><br> 
    <p>Comments: 
     <textarea name="comments" id="textarea" cols="45" rows="5"required></textarea> 
    </p> 
<br><br> 
    <p> 
     <input type="checkbox" name="Mailing List" id="checkbox" onClick = "showMailingListForm()"> 
     Please put me on your mailing list. 
    </p> 
<div id = "mailingInformation"> 
<h3>Please fill out the form below to be put on the mailing list to recieve coupons and special offers</h3> 
    <p>Check the box to use address above 
    <input type = "checkbox" name = "checkForAddress" id = "checkAddress"> 
    </p> 
    <p>First Name: 
     <input type = "text" name = "mailingName" id = "mailing"> 
    </p> 
    <p>Last Name: 
     <input type = "text" name = "mailingLastName" id = "mailingLast"> 
    </p> 
    <p>Mailing Address: 
     <input type = "text" name = "mailingAddress" id = "mailingAdd"> 
    </p> 
    <p>Phone Number(Optional) 
     <input type = "text" name = "phoneNumber" id = "phone"> 
    </p> 
</div> 
    <p> 
     <input type="checkbox" name="More Info" id="checkbox2"> 
     Send me more information about your products.</label> 
    </p> 
<br><br> 
    <p> 
    <input type="hidden" name="hiddenField" id="hidden" value="application-id:US447"> 
    </p> 

<br><br> 

    <p> 
    <input type="submit" name="button" id="button" value="Submit"> 
    <input type="reset" name="button2" id="button2" value="Reset"> 
    </p> 
<div> 
</form> 
<div id = "hiddenStuff"> 
<p> 
    <table border='a'> 
    <tr> 
     <th>Field Name</th> 
     <th>Value of Field</th> 
    </tr> 
    <?php echo $tableBody; ?> 
    </table> 
<!--</p> 
<p>Name: <?php echo $inName; ?></p> 
<p>Email: <?php echo $inEmail; ?></p> 
<p>Address: <?php echo $inAddress; ?></p> 
<p>Reason: <?php echo $inReason; ?></p> 
<p>Comments: <?php echo $inComments; ?></p> 
<p>Mailing List: <?php echo $inMailBox; ?></p> 
<p>Use Previous Address Given: <?php echo $inUseAddress; ?></p> 
<p>First Name: <?php echo $inFirstName; ?></p> 
<p>Last Name?: <?php echo $inLastName; ?></p> 
<p>Mailing Address: <?php echo $inMailingAdd; ?></p> 
<p>Phone Number: <?php echo $inPhoneNumber; ?></p> 
<p>More Information: <?php echo $inMoreInfo; ?></p>--> 
</div> 
</body> 




</html> 

某些代码行已被注释掉,以便进行实验。感谢您的帮助。

+0

首先检查是否正在提交像'$ _POST ['电子邮件地址']或东西使用'if'子句。 – Rasclatt

回答

1

这是因为这样:

<?php 
    $toEmail = "[email protected]";  //CHANGE within the quotes. Place email address where you wish to send the form data. 
             //Use your DMACC email address for testing. 
             //Example: $toEmail = "[email protected]";   

    $subject = "WDV341 Email Example"; //CHANGE within the quotes. Place your own message. For the assignment use "WDV101 Email Example" 

    $fromEmail = "[email protected]";  //CHANGE within the quotes. Use your DMACC email address for testing OR 
             //use your domain email address if you have Heartland-Webhosting as your provider. 
             //Example: $fromEmail = "[email protected]"; 

// DO NOT CHANGE THE FOLLOWING LINES // 

    $emailBody = "Form Data\n\n ";   //stores the content of the email 
    foreach($_POST as $key => $value)  //Reads through all the name-value pairs. $key: field name $value: value from the form         
    { 
     $emailBody.= $key."=".$value."\n"; //Adds the name value pairs to the body of the email, each one on their own line 
    } 

    $headers = "From: $fromEmail" . "\r\n";    //Creates the From header with the appropriate address 

    if (mail($toEmail,$subject,$emailBody,$headers)) //puts pieces together and sends the email to your hosting account's smtp (email) server 
    { 
     echo("<p>Message successfully sent!</p>"); 
    } 
    else 
    { 
     echo("<p>Message delivery failed...</p>"); 
    } 
?> 

你必须检查,如果你的form提交,则上面的代码执行。所以,把上面的代码中:

if(isset($_REQUEST['form_element_index'])) 
{ 
    // Above code here 
    // Now the code executes when form is submitted 
} 
+0

停止发送电子邮件并允许我在发送电子邮件之前填写表单。但现在它不会让我提交信息。我可以点击提交,没有任何反应。我知道这个按钮的作用,因为当我点击它而没有填写任何表单域时,表单会要求你填写一个域,因为我已经验证了表单。奇怪的东西! –

+0

因此,这是什么错,是不是正常的流量? –

+0

你确定了我的正确道路。由于你的代码让我更靠近了一步,所以我知道你是对的,所以我做了一些更多的研究。我发现它是错误的isset。我用isset($ _ POST [])并且完美地工作。谢谢你的帮助,把我送到正确的道路上!你摇滚的朋友 –

0

它的发生,因为你have't创建一个表单,并要求用户给你input.What所要做的就是创建一个表单,然后retreive形式值并在提交形式发送邮件。它肯定会工作....