gpt4 book ai didi

javascript - jQuery ajax 发布数据以形成邮件

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

我试图让 jQuery 通过 Ajax 将一些数据发布到 PHP 表单邮件中,这样我就可以在电子邮件中发送数据。但我的问题是它没有在邮件中发布任何内容。里面是空的。

这是我的 JavaScript:

$(document).ready(function() {
$('#food_requirements').hide();
$('#name_allergy').hide();
$('#food').click(function(e) {
$('#food_requirements').fadeIn('slow');
});
$('#allergy').click(function(e) {
$('#name_allergy').fadeIn('slow');
});

$('.confirm input').click(function(e) {
$(".confirm").removeClass('red-border');
});
$('.checkin').click(function() {
$(this).attr("src", 'images/loader.gif');
var checkBoxs = document.getElementsByName("confirmation");
var confirmation = '';
for (var i = 0; i < checkBoxs.length; i++) {
if (checkBoxs[i].checked) {
confirmation += checkBoxs[i].value + " - ";
}
}
if (confirmation == '') {
$('.confirm').addClass('red-border');
$(this).attr("src", 'images/checkin.jpg');
return false;
}
if (document.getElementById("food").checked) {
var food = "yes";
} else {
var food = "no";
}
var food_requirements = $('#food_requirements').val();
if (food == 'yes') {
if (food_requirements == '') {
$('#food_requirements').addClass('red-border');
$('#food_requirements').focus();
$(this).attr("src", 'images/checkin.jpg');
return false;
} else {
$("#food_requirements").removeClass('red-border');
}
}
if (document.getElementsByName("allergy").checked) {
var allergy = "yes";
} else {
var allergy = "no";
}
var name_allergy = $('#name_allergy').val();
if (allergy == 'yes') {
if (name_allergy == '') {
$('#name_allergy').addClass('red-border');
$('#name_allergy').focus();
$(this).attr("src", 'images/checkin.jpg');
return false;
} else {
$("#name_allergy").removeClass('red-border');
}
}
var firstname1 = $('#firstname1').val();
if (firstname1 == '') {
$('#firstname1').addClass('red-border');
$('#firstname1').focus();
$(this).attr("src", 'images/checkin.jpg');
return false;
} else {
$("#firstname1").removeClass('red-border');
}
var lastname1 = $('#lastname1').val();
if (lastname1 == '') {
$('#lastname1').addClass('red-border');
$('#lastname1').focus();
$(this).attr("src", 'images/checkin.jpg');
return false;
} else {
$("#lastname1").removeClass('red-border');
}
var age1 = $('#age1').val();
if (age1 == '') {
$('#age1').addClass('red-border');
$('#age1').focus();
$(this).attr("src", 'images/checkin.jpg');
return false;
} else {
$("#age1").removeClass('red-border');
}
var fname = "";
var firstname = document.getElementsByName("firstname");
for (var i = 0; i < firstname.length; i++) {
if (firstname[i].value != '') {
if (firstname[i].value != '') fname += firstname[i].value + " / ";
}
}
var lname = "";
var lastname = document.getElementsByName("lastname");
for (var i = 0; i < lastname.length; i++) {
if (lastname[i].value != '') lname += lastname[i].value + " / ";
}
var ages = "";
var age = document.getElementsByName("age");
for (var i = 0; i < age.length; i++) {
if (age[i].value != '') ages += age[i].value + " / ";
}










var data = "&confirmation=Events: " + confirmation + "&firstname=First names: " + fname + "&lastname=Last names: " + lname + "&age=Ages: " + ages;
if (food_requirements != '') data = data + "&food_requirements=Food requirements: " + food_requirements;
if (name_allergy != '') data = data + "&allergy=Allergy: " + name_allergy;
var url = "rsvp.php";
$('#form').html('Your CHECK-IN is done!.<br/> Thank you very much.');
$.post(url, data, function(msg) {
$('#form').addClass('formfinal');
alert(data)
window.scrollTo(0, 0);
if (msg.substr(0, 2) == "ok") {
$('#form').html('Your CHECK-IN is done!.<br/> Thank you very much.');
} else {
$('#form').html('There has been an error with the CHECK-IN. Please try again.');
return false;
}
});





});
});
function isEmail(email) {
var regex = /^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/;
return regex.test(email);
}
function addguest() {
$("#guests").append("<input class='firstname' name='firstname' placeholder='First Name' /><input class='lastname' name='lastname' placeholder='Last Name' /><input class='age' name='age' placeholder='Age' /><div class='clr'></div>");
}

当我提醒“数据”时,我得到这个值

"&confirmation=事件:宝莱坞 - &firstname=名字:andreas/john/james/&lastname=姓氏:Johnson/Jamesson/Hansen/&age= 年龄:24/26/42/&food_requirements=食物要求:有效吗”

这是我的 php(rsvp.php) 表单。我尝试了一些不同的东西只是为了获得某种值(value),但没有任何运气。我刚刚收到一封空电子邮件。

<?php
$email = 'from@mail.com';
$message = 'firstname' . $firstname . '';
$to = 'me@mail.com';
mail($to, 'my subject', $message, $email);
?>

请让我知道该怎么做。

最佳答案

问题就在这里你期待 $firstname = $_POST['fname'];但传递名字

var data = "&confirmation=Events: " + confirmation + "&firstname=First names: " + fname + "&lastname=Last names: " + lname + "&age=Ages: " + ages;

关于javascript - jQuery ajax 发布数据以形成邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23686467/

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