gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 04:36:12 27 4
gpt4 key购买 nike

我有一个自发布文档,其中包含表单字段,需要在同一文档上使用 php 处理来填写。我的问题是,打开页面(网站)时,显示消息“消息已发送!”在填写信息之前立即显示。 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>

为了进行实验,一些代码行已被注释掉。感谢您的帮助。

最佳答案

这是因为:

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

您必须检查您的表单是否已提交,然后执行上述代码。所以将上面的代码放入:

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

关于javascript - php mail() 在填写表单之前发送邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43290114/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com