我在 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 );
?>
最佳答案
jQuery 的 $.ajax
的选项中没有 event
属性。
您可能想通过表单发送该数据
$(document).ready(function() {
$("form").submit(function() {
var event_data = encodeURIComponent( $("#event").text() );
$.ajax({
type: "POST",
url: 'contact.php',
data: $(this).serialize() + "&event=" + event_data,
success: function(data) {
$('#results').html(data);
},
error: function(jqXHR, text, error) {
$('#result').html(error);
}
});
return false;
});
});
关于javascript - 通过 php post 在 html dom 中发送文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35317232/