留言板验证js+php+mySQL
//表单验证
function verify(){
//form提交之前先判空
var mfzdlj = $(".mfzdlj").val().trim();
var mfname = $("#mfuser").val().trim();
var mfphone =$("#mfphone").val().trim();
if(mfzdlj==""){
tip("店铺链接不能为空!");
return false;
}
if(mfname == ""){
tip("姓名不能为空!");
return false;
}
if(mfphone==""){
tip("手机不能为空");
return false;
}
}
//底部业务资讯判断
function ywzx(){
//form提交之前先判空
var ywusers = $("#ywusers").val().trim();
var ywnums = $("#ywnums").val().trim();
if(ywusers==""){
tip("姓名不能为空!");
return false;
}else if(!CheckChinese(ywusers)){
//console.log(CheckChinese(ywusers))
tip("姓名必须是中文!");
return false;
}
if(ywnums == ""){
tip("联系方式不能为空");
return false;
}
}
//正则表达式中文验证:
function CheckChinese(str){
var reg = new RegExp("[\\u4E00-\\u9FFF]+","g");
return reg.test(str);
}
//底部ajax数据提交
function postFrom(e){
e.preventDefault();
var str = document.getElementById("ywusers").value;
var age2 = document.getElementById("ywnums").value;
//console.log(age2);
var params ="users="+str+"&"+"nums="+age2;
//var params ="name="+str;
var xhr = new XMLHttpRequest();
xhr.open("POST","send.php",true);
//设置请求头
xhr.setRequestHeader('Content-type','application/x-www-form-urlencoded');
xhr.onload = function(){
//console.log(this.responseText);
tip("留言成功");
ywzx();
}
xhr.send(params);
};
//店铺免费诊断
function mpostFrom(e){
e.preventDefault();
var mfuser = document.getElementById("mfuser").value;
var mfphone = document.getElementById("mfphone").value;
var mfzdlj = document.querySelector(".mfzdlj").value;
//console.log(age2);
var params ="txt="+mfzdlj+"user="+mfuser+"&"+"num="+mfphone;
//var params ="name="+str;
var xhr = new XMLHttpRequest();
xhr.open("POST","comment.php",true);
//设置请求头
xhr.setRequestHeader('Content-type','application/x-www-form-urlencoded');
xhr.onload = function(){
//console.log(this.responseText);
tip("留言成功");
verify();
}
xhr.send(params);
};
//弹窗
function tip(cnbtxt){
var tip = document.querySelector(".tip");
var tipClose = document.querySelector(".tip-close");
var tipmark = document.querySelector("#mark");
var tipTxt = document.querySelector(".tip-txt");
var Que = document.querySelector(".que");
tipTxt.innerHTML = cnbtxt;
tip.style.display = "block";
tipmark.style.display = "block";
tipClose.onclick = function(){
tipclose();
};
Que.onclick = function(){
tipclose();
}
function tipclose(){
tip.classList.add('tipon');
tip.addEventListener('animationend',function(){
//动画结束,移除动画的样式类
tip.classList.remove('tipon');
tip.style.display = "none";
tipmark.style.display = "none";
document.getElementById("mfuser").value ="";
document.getElementById("mfphone").value="";
document.querySelector(".mfzdlj").value ="";
});
}
}
//底部业务资讯ajax数据提交
var opostFrom = document.querySelector("#postFrom");
opostFrom.addEventListener("submit",postFrom,true);
//店铺免费诊断ajax数据提交
var mPostFrom = document.querySelector("#mpostFrom");
mPostFrom.addEventListener("submit",mpostFrom,true);