您当前的位置: 首页 > 学无止境 > JS经典实例 网站首页JS经典实例
jquery使用ajax向php提交实例
发布时间:2015-11-28 10:40:59编辑:雪饮阅读()
js部分:
function submitOrder(src, _oid) {
showProc(src);
var _biaoti=$j("biaoti").val();
var _contact = $j("txtContact").val();
var _compName = $j("txtCompName").val();
var _tel = $j("txtTel").val();
var _mobile = $j("txtMobile").val();
var _email = $j("txtEmail").val();
var _addr = $j("txtAddr").val();
var _content = $j("txtContent").val();
var errorMsg = "";
if (_contact.length == 0 || _contact == "请填写联系人") {
errorMsg += "<p>联系人不可为空</p>";
}
if (_mobile.length == 0) {
errorMsg += "<p>手机不可为空</p>";
}
var ptns = /^\d{11,13}$/;
if (_mobile.length > 0 && !ptns.test(_mobile)) {
errorMsg += "<p>手机格式错误</p>";
}
var ptn = /\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
if (_email.length > 0 && !ptn.test(_email)) {
errorMsg += "<p>E-Mail格式错误</p>";
}
if (_content.length == 0 || _content == "请填写订购的产品数量和产品描述,方便我们进行统一备货") {
errorMsg += "<p>采购意向描述不可为空</p>";
}
if (errorMsg.length > 0) {
$a(errorMsg);
showProc(src, false);
return;
}
/*$.post("/api/diy.php", {
biaoti: _biaoti,
lxr: _contact,
//compName: _compName,
//tel: _tel,
sjhm: _mobile,
dzyd: _email,
//addr: _addr,
miaoshu: _content
}, function(msg) {
var sta = gav(msg, "state");
var sMsg = gav(msg, "msg");
if (sta == "1") {
$a(sMsg, 1);
emptyText('tbForm1');
} else {
$a(msg);
emptyText('tbForm1');
}
});
showProc(src, false);
*/
$.post("/api/diy.php",
{
biaoti: _biaoti,
lxr: _contact,
sjhm: _mobile,
dzyd: _email,
miaoshu: _content
},
function(data,status){
alert("数据:" + data + "\n状态:" + status);
});
}
php代码(调用了dede中的数据库类,根据情况可以改成原生php接收):
<?php
require_once ($_SERVER['DOCUMENT_ROOT'] . "/include/common.inc.php");
$biaoti=$_POST["biaoti"];
$lxr=$_POST["lxr"];
$sjhm=$_POST["sjhm"];
$dzyd=$_POST["dzyd"];
$miaoshu=$_POST["miaoshu"];
$sql = "INSERT INTO `dede_diyform1`(biaoti,lxr,sjhm,dzyd,miaoshu) VALUES('".$biaoti."','".$lxr."','".$sjhm."','".$dzyd."','".$miaoshu."');";
$dsql->ExecuteNoneQuery($sql);//执行SQL操作
$gid = $dsql->GetLastID();//获取刚刚插入的id
echo $gid;
?>
关键字词:jquery,ajax
上一篇:利用iframe模拟ajax提交
下一篇:jquery选色板(js换肤)