2017-04-08 64 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形式值並在提交形式發送郵件。它肯定會工作....