我通过执行Php函数从我的html页面发送邮件,我的index.html:
<form method="POST" action="sendmail.php" >
<input type="text" name="sender_name" placeholder="Name" required="">
<input type="text" name="sender_email" placeholder="Email" required="">
<input type="text" name="subject" placeholder="Subject" required="">
<textarea placeholder="Message" name="message" required=""></textarea>
<input type="submit" name="send" value="SEND">
</form>
我的sendmail.php:
<?php
if($_POST['send'] == 'SEND'){
$to = 'queries@mydomain.com';
$subject = $_POST['subject'];
$message = $_POST['message'];
$headers = "From: ".$_POST['sender_name']." <".$_POST['sender_email'].">\r\n"; $headers = "Reply-To: ".$_POST['sender_email']."\r\n";
$headers = "Content-type: text/html; charset=iso-8859-1\r\n";
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
}
?>
以上工作正常,但它也离开当前页面我想在当前页面上执行上述方法,并根据此question&amp;这question
我必须使用 Ajax 和 jQuery ?到目前为止我对他们一无所知我有这个:
////in my index.html
<button type="button">send mail</button>
<p></p>
<script type="text/javascript">
$(document).ready(function(){
$("button").click(function(){
$.ajax({
type: 'POST',
url: 'sendmail.php',
success: function(data) {
alert(data)
$("l").text(data);
}
});
});
});
</script>
我知道上面的不正确&amp;它不工作(目前它在弹出窗口中显示sendmail.php的文本)我怎样才能使它工作,以便我可以执行sendmail.php ???
更新:
的index.html
<form method="POST" id="myForm" action="sendmail.php">
<input type="text" name="sender_name" placeholder="Name" required="">
<input type="text" name="sender_email" placeholder="Email" required="">
<input type="text" name="subject" placeholder="Subject" required="">
<textarea placeholder="Message" name="message" required=""></textarea>
<input type="submit" name="send" value="SEND">
</form>
</div>
<script type="text/javascript">
$(document).ready(function(){
$("#myForm").on('submit', function(){
var formData = $(this).serialize();
$.ajax({
type: 'POST',
url: 'sendmail.php',
data: formData,
success: function(response.success) {
alert(response.success)
$("l").text(response.success);
}
});
});
</script>
`
答案 0 :(得分:5)
以下内容适用于.on('submit'
和.serialize()
<强> HTML 强>
<form method="POST" id="myForm" action="sendmail.php">
<input type="text" name="sender_name" placeholder="Name" required="">
<input type="text" name="sender_email" placeholder="Email" required="">
<input type="text" name="subject" placeholder="Subject" required="">
<textarea placeholder="Message" name="message" required=""></textarea>
<input type="submit" name="send" value="SEND">
</form>
<强> JS 强>
$(document).ready(function() {
$("#myForm").on('submit', function(event) {
event.preventDefault();
var formData = $(this).serialize();
$.ajax({
type: 'POST',
url: 'sendmail.php',
dataType: "json",
data: formData,
success: function(response) {
alert(response.success);
},
error: function(xhr, status, error){
console.log(xhr);
}
});
});
});
<强> PHP 强>
<?php
if(isset($_POST['message'])){
$to = 'queries@mydomain.com';
$subject = $_POST['subject'];
$message = $_POST['message'];
$headers = "From: ".$_POST['sender_name']." <".$_POST['sender_email'].">\r\n"; $headers = "Reply-To: ".$_POST['sender_email']."\r\n";
$headers = "Content-type: text/html; charset=iso-8859-1\r\n";
'X-Mailer: PHP/' . phpversion();
if(mail($to, $subject, $message, $headers)) echo json_encode(['success'=>true]);
else echo json_encode(['success'=>false]);
exit;
}
?>
答案 1 :(得分:3)
$(document).ready(function(){
$("#my_button").click(function(){
$.ajax({
type: 'POST',
url: 'sendmail.php',
data : $('#my_form').serialize(),
success: function(data) {
}
});
});
jQuery代码
<?php
$searcharray = array();
parse_str($_POST['data'], $searcharray);
if($searcharray['send'] == 'SEND'){
$to = 'queries@mydomain.com';
$subject = $searcharray['subject'];
$message = $searcharray['message'];
$headers = "From: ".$searcharray['sender_name']." <".$searcharray['sender_email'].">\r\n";
$headers .= "Reply-To: ".$searcharray['sender_email']."\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= 'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
}
Php代码
class Person(models.Model):
identity_number = models.CharField(max_length=20)
identity_type = models.IntegerField()
class Student(Person):
student_number = models.CharField(max_length=20)
class Employee(Person):
employee_number = models.CharField(max_length=20)
答案 2 :(得分:2)
请为您的表单标记提供id和id,如下面的代码。
<form method="POST" action="sendmail.php" id="sendForm">
<input type="text" name="sender_name" placeholder="Name" required="">
<input type="text" name="sender_email" placeholder="Email" required="">
<input type="text" name="subject" placeholder="Subject" required="">
<textarea placeholder="Message" name="message" required="">
</textarea>
<input type="submit" name="send" value="SEND" id="submitButton">
</form>
然后像下面的代码一样更新你的脚本
$(document).ready(function(){
$("#submitButton").click(function(event){
event.preventDefault();
var datas = $('#sendForm').serialize();
$.ajax({
type: 'POST',
url: 'sendmail.php',
data: datas,
success: function(data) {
alert(data)
$("l").text(data);
}
});
});
});
检查此documentation,了解如何使用ajax
提交表单数据答案 3 :(得分:2)
您的表单为id
并使用JS
preventDefault()
<form method="POST" id="myForm" action="sendmail.php">
<input type="text" name="sender_name" placeholder="Name" required="">
<input type="text" name="sender_email" placeholder="Email" required="">
<input type="text" name="subject" placeholder="Subject" required="">
<textarea placeholder="Message" name="message" required=""></textarea>
<input type="submit" id="sendMail" name="send" value="SEND">
</form>
sendmail.php
<?php
if($_POST['send'] == 'SEND'){
$to = 'queries@mydomain.com';
$subject = $_POST['subject'];
$message = $_POST['message'];
$headers = "From: ".$_POST['sender_name']." <".$_POST['sender_email'].">\r\n"; $headers = "Reply-To: ".$_POST['sender_email']."\r\n";
$headers = "Content-type: text/html; charset=iso-8859-1\r\n";
'X-Mailer: PHP/' . phpversion();
//check if the mail was sent
if (mail($to, $subject, $message, $headers)) {
echo json_encode(['result' => true]);
}else {
echo json_encode(['result' => false]);
}
}
?>
的jQuery
$(document).ready(function () {
//send button click
$('#sendMail').on('click', function (e) {
e.preventDefault();
$.post('sendmail.php', $('#myForm').serialize(), function (data) {
//show the results if the mail was sent or not
var res = $.parseJSON(data);
if (res.result === true) {
$('#feedback').html('your mail was sent');
}else {
$('#feedback').html('your mail was not sent');
}
});
});
});