2017-04-08 16 views
0

私は、同じ文書のPHP処理で記入するフォームフィールドを持つ自己転記文書を持っています。私の問題は、ページ(ウェブサイト)を開くと、「メッセージが送信されました!」というメッセージです。フォームに情報を書き込むことができる直前に表示されます。 php mail()関数は私の電子メールアカウントにリンクされているので、フォームデータの電子メールを取得します。フォームが記入される前に電子メールが送信されたため、データは送信されません。フォームが実際の情報を送信するように、電子メールが送信される前にフォームに記入できるようにしたい。 Iveはこの話題を研究し、何も出てこなかった。どんな助けも素晴らしいだろう!ここに私のコードです...php mail()フォームが入力される前にメールを送信

<?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 ['Email Address']'や何かが '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は上記のコードを実行後、提出されたかどうかを確認する必要があります。フォームを作成し、あなたがしなければならないinput.What与えることをユーザーに尋ねhave'tためのフォームを作成し、フォームの値を盗んし、提出時にされ

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

電子メールの送信が停止され、電子メールが送信される前に書類に記入することができます。しかし今、私は情報を提出させません。送信をクリックすると何も起こりません。フォームフィールドを入力せずにクリックすると、フォームが検証されているためフォームにフィールドを入力するよう要求するため、ボタンが機能することがわかります。奇妙なもの! –

+0

これで何が間違っていますか、それは正常な流れではありませんか? –

+0

あなたは確かに正しい道に私を置いてきました。あなたのコードが私を一歩近づけてくれたので、私はあなたがissetについて正しいことを知っていましたので、私はもう少し詳しく研究しました。私はそれが間違っていることを知った。私はisset($ _ POST [])を使用しました。私に正しい道を送ってくれてありがとう!あなたはロックバディ –

0

そのが起こっ:だからで上記のコードを置きますフォームはメールを送信します。これは間違いなく動作します....

関連する問題