当我将我的联系表单中的数据以数组格式提交给邮件时,我想将该数据作为普通文本。在此代码中,从谷歌脚本中发送本地邮件。你能帮我解决一下吗?
这是我的输出:
" {"name":["ganesh"],"message":["vndsjkvbkjd,v "],"email":["mine@gmail.com"]} "
我希望输出像
" name : ganesh , message : welcome , email : mine@gmail.com"
我的代码如下:
function validEmail(email) { // see:
var re = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
return re.test(email);
}
// get all data in form and return object
function getFormData() {
var elements = document.getElementById("gform").elements; // all form elements
var fields = Object.keys(elements).filter(function(k){
return k.length > 1 && elements[k].name && elements[k].name.length > 0 ;
});
var data = {};
fields.forEach(function(k){
data[k] = elements[k].value;
});
console.log(data);
return data;
}
function handleFormSubmit(event) { // handles form submit withtout any jquery
event.preventDefault(); // we are submitting via xhr below
var data = getFormData(); // get the values submitted in the form
if( !validEmail(data.email) ) { // if email is not valid show error
document.getElementById('email-invalid').style.display = 'block';
return false;
} else {
var url = event.target.action; //
var xhr = new XMLHttpRequest();
xhr.open('POST', url);
// xhr.withCredentials = true;
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.onreadystatechange = function() {
console.log( xhr.status, xhr.statusText )
console.log(xhr.responseText);
document.getElementById('gform').style.display = 'none'; // hide form
document.getElementById('thankyou_message').style.display = 'block';
return;
};
// url encode form data for sending as post data
var encoded = Object.keys(data).map(function(k) {
return encodeURIComponent(k) + '=' + encodeURIComponent(data[k])
}).join('&')
xhr.send(encoded);
}
}
function loaded() {
console.log('contact form submission handler loaded successfully');
// bind to the submit event of our form
var form = document.getElementById('gform');
form.addEventListener("submit", handleFormSubmit, false);
};
document.addEventListener('DOMContentLoaded', loaded, false);
答案 0 :(得分:0)
嗯,我想这只是输出格式,你可能无法改变它......你能做的就是编写一个函数,将你给出的输出转换成你想要的输出