Javascript php mail()在填写表单之前发送邮件

Javascript php mail()在填写表单之前发送邮件,javascript,php,html,Javascript,Php,Html,我有一个自我发布文档,它有表单字段,可以在同一文档上用php处理来填写。我的问题是,在打开页面(网站)时,“message Sent!”消息会在表单填写信息之前立即显示。php mail()函数链接到我的电子邮件帐户,因此我可以从电子邮件中获取表单数据。但是没有发送数据,因为电子邮件是在填写表格之前发送的。我希望能够在电子邮件发送前填写表格,以便表格发送实际信息。我研究过这个话题,什么也没想到。任何帮助都会很棒!这是我的密码 <?php foreach($_POST as $key

我有一个自我发布文档,它有表单字段,可以在同一文档上用php处理来填写。我的问题是,在打开页面(网站)时,“message Sent!”消息会在表单填写信息之前立即显示。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 = "robinjennings@nephilim42.com";      //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 = "robinjennings@nephilim42.com";        //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>

身体{
背景图片:url(“rbGrid.png”);
背景大小:150%;
背景重复:无重复;
文本对齐:居中;
}
div{
背景色:黑色;
不透明度:0.9;
颜色:白色;
文本对齐:居中;
}
h1{
颜色:白色;
}
氢{
颜色:白色;
}
#边界样式{
花边:重瓣厚红色;
边界半径:45px;
宽度:50%;
保证金:0自动;
}
#隐藏的东西{
显示:无;
}
文本区{
显示:无;
保证金:0自动;
}
#邮寄信息{
显示:无;
保证金:0自动;
}
桌子{
保证金:0自动;
边框:实心厚红色;
边界半径:20px;
}
th{
边框:实心厚红色;
边界半径:45px;
}
tr{
颜色:白色;
边框:实心细红色;
边界半径:45px;
}
运输署{
颜色:白色;
边框:实心细红色;
边界半径:45px;
}
函数showProductProblemComments()
{
document.getElementById(“textarea”).style.display=“block”;
}
函数showMailingListForm()
{
document.getElementById(“mailingInformation”).style.display=“block”;
}

这是因为:

<?php
    $toEmail = "robinjennings@nephilim42.com";      //CHANGE within the quotes. Place email address where you wish to send the form data. 
                                        //Use your DMACC email address for testing. 
                                        //Example: $toEmail = "jhgullion@dmacc.edu";        

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

    $fromEmail = "robinjennings@nephilim42.com";        //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 = "contact@jhgullion.org";  

//   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>");
    }
?>

发生这种情况是因为您尚未创建表单并要求用户提供输入。您需要做的是创建表单,然后检索表单值,并在提交表单时发送邮件。这肯定会起作用。…

首先检查提交的内容,如
$\u POST['Email Address']
或使用
if
子句的内容。这会阻止文档发送电子邮件,并允许我在发送电子邮件之前填写表单。但现在它不让我提交信息。我可以点击提交,但什么也没发生。我知道这个按钮是有效的,因为当我单击它而不填写任何表单字段时,表单会要求您填写一个字段,因为我已经验证了表单。奇怪的东西!你让我走上了正确的道路。因为你的代码让我更进一步,我知道你对isset的看法是正确的,所以我做了更多的研究。我发现这是一个错误的isset。我使用了isset($_POST[]),效果非常好。谢谢你的帮助,让我走上正确的道路!您可以使用
$\u POST[]
完整地表示POST数组,当您使用
$\u POST['form\u element\u index']
时,表示您正在检查表单中的单个html元素,这就是区别。@RobinJennings:如果你认为答案对你有帮助,请接受并向上投票。我单击了“喜欢”按钮,它说它被录制了,但由于我的声誉很低,所以不会显示类似的内容。我使用了,如果(isset($_POST['submit']){,它就像一个符咒一样有效!
if( isset($_REQUEST['form_element_index']) )
{
     // Above code here
     // Now the code executes when form is submitted 
}