【发布时间】:2016-05-20 22:11:31
【问题描述】:
我在 html5 中有两个表单,它们有一个标题(事件)。当用户订阅一个事件时,我会发送两封电子邮件:一封给我自己的邮件,一封给用户的邮件。在给用户的邮件中,我会写他已成功订阅。它有效。
但我还想在电子邮件中添加事件的名称(h1 标题的内容)。我能怎么做?实际上它在电子邮件消息中的“事件:”中留下了一个空白字段。
索引.php:
<form name="contact1" action="index.php" method="POST" id="contact1">
<h1 id="event">Event 1</h1>
<div>Name: <input type="text" name="name" id="name" required /></div>
<div>Email: <input type="email" name="email" id="email" required /></div>
<div><input type="submit" name="submit" value="Submit" /></div>
</form>
<form name="contact2" action="index.php" method="POST" id="contact2">
<h1 id="event">Event 2</h1>
<div>Name: <input type="text" name="name" id="name" required /></div>
<div>Email: <input type="email" name="email" id="email" required /></div>
<div><input type="submit" name="submit" value="Submit" /></div>
</form>
<div id="results"></div>
在关闭body 之前,我插入jquery 代码以将值发送到contact.php
$(document).ready(function() {
$("form").submit(function() {
// Getting the form ID
var formID = $(this).attr('id');
var formDetails = $('#'+formID);
$.ajax({
type: "POST",
url: 'contact.php',
event:$("#event").text(),
data: formDetails.serialize(),
success: function (data) {
// Inserting html into the result div
$('#results').html(data);
},
error: function(jqXHR, text, error){
// Displaying if there are any errors
$('#result').html(error);
}
});
return false;
});
});
这是contact.php代码:
<?php
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
if(isset($_POST['email'])){
$responso = "Success: request sent.";
$email_to = "admin@admin.com"; // Send to admin
$email_subject = "New user"; // Object Email to admin
$first_name = $_POST['name']; // name of user
$email_from = $_POST['email']; // email of user
$event = $_POST['event']; // name of the event
$email_message .= "Name: ".clean_string($first_name)."\n";
$email_message .= "Email Address: ".clean_string($email_from)."\n";
$email_message .= "Evento: ".clean_string($event)."\n";
// First mail to admin
$headers = 'From: '.$email_from."\r\n". 'Reply-To: '.$email_from."\r\n" . 'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
// Second mail to new user
$email_subject2 = "Confirmation"; // Object Email to new user
$email_message2 .= "Successful subscribed to ".clean_string($event)."\n";
$email_message2 .= "dear ".clean_string($first_name)."\n";
$headers2 = 'From: '.$email_to."\r\n". 'Reply-To: '.$email_to."\r\n" . 'X-Mailer: PHP/' . phpversion();
@mail($email_from, $email_subject2, $email_message2, $headers2);
}else{
$responso = "Error: Insert your email";
}
die( $responso );
?>
【问题讨论】:
标签: javascript php jquery html email