PHP jQuery ajax 表单提交小示例(含insert, select)

功能描述:能够通过表单向MySQL数据库新增记录,能够表单提供关键词进行查询

index.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>货物销售记录</title>
<script src="JS/jquery-1.8.2.min.js"></script>
<script language="javascript" src="JS/function.js"></script>
<link rel="stylesheet" type="text/css" href="css/style.css"/>
</head>

<body>
<p><strong>销售方功能模拟:</strong></p>
<p>登记销售记录>></p>
<table width="500" border="1">
<tr>
<td width="130">客户姓名</td>
<td width="194">
<input type="text" name="txtUserName" id="txtUserName" /></td>
<td width="154"> </td>
</tr>
<tr>
<td>客户电话</td>
<td>
<input type="text" name="txtUserTel" id="txtUserTel" /></td>
<td> </td>
</tr>
<tr>
<td>购货日期</td>
<td>
<input type="text" name="txtDate" id="txtDate" /></td>
<td> </td>
</tr>
<tr>
<td>货物编号</td>
<td>
<input type="text" name="txtGoodsID" id="txtGoodsID" /></td>
<td> </td>
</tr>
<tr>
<td>货物名称</td>
<td>
<input type="text" name="txtGoodsName" id="txtGoodsName" /></td>
<td> </td>
</tr>
<tr>
<td>购货数量</td>
<td>
<input type="text" name="txtGoodsNum" id="txtGoodsNum" /></td>
<td> </td>
</tr>
<tr>
<td colspan="3"><input type="button" name="button" id="button" value="保存" /><div id="insertStatus">这里显示结果</div></td>
</tr>
</table>
<div class="divBorder">
<p>查询销售记录>></p>
<p>
<input type="button" name="button2" id="button2" value="显示全部" />
<input type="button" name="button4" id="button4" value="隐藏结果" />
</p>

<p>选择类别:
<select name="queryField" id="queryField">
<option value="userName">客户姓名</option>
<option value="userTel">客户电话</option>
<option value="goodsID">货物编号</option>
<option value="goodsName">货物名称</option>
<option value="tradeDate">购物日期</option>
</select>
</p>
<p>查询值:
<input type="text" name="queryKeywords" id="queryKeywords" />
</p>
<p>
<input type="button" name="button3" id="button3" value="搜索" />
</p>
<div id="queryResult"> 这里显示销售方查询结果</div>
</div>
<hr />
<p><strong>顾客方功能模拟:</strong></p>
<div class="divBorder">
<p>货物验证>></p>
<table width="500" border="1">
<tr>
<td width="130">客户姓名</td>
<td width="194">
<input type="text" name="txtUserName2" id="txtUserName2" />
*</td>
<td width="154"> </td>
</tr>
<tr>
<td>客户电话</td>
<td>
<input type="text" name="txtUserTel2" id="txtUserTel2" />
*</td>
<td> </td>
</tr>
<tr>
<td>货物编号</td>
<td>
<input type="text" name="txtGoodsID2" id="txtGoodsID2" />
*</td>
<td> </td>
</tr>
<tr>
<td colspan="3"><input type="button" name="button5" id="button5" value="查询" />
<span id="insertStatus2"></span></td>
</tr>
</table>
<div id="validateResult">这里显示验证结果</div>
</div>
</body>
</html>

index.html效果图

function.js

// JavaScript Document

//插入记录
$(function(){
$("#button").click(function(){
$("#insertStatus").text("正在保存,请稍候..."); //显示提醒
//获取用户值
txtUserName=$("#txtUserName").val();
txtUserTel=$("#txtUserTel").val();
txtDate=$("#txtDate").val();
txtGoodsID=$("#txtGoodsID").val();
txtGoodsName=$("#txtGoodsName").val();
txtGoodsNum=$("#txtGoodsNum").val();
//以ajax方式与后台程序交互
$.ajax({
url:'insert.php',
type:'post',
dataType:'html',
data:{postUserName:txtUserName,postUserTel:txtUserTel,postGoodsID:txtGoodsID,postGoodsName:txtGoodsName,postGoodsNum:txtGoodsNum,postDate:txtDate},
success:function(data)
{
if(data==1)
$("#insertStatus").text("保存成功");
else
$("#insertStatus").text("保存失败");
}
})
})
})

// 查询全部记录
$(function(){
$("#button2").click(function(){
$("#queryResult").text("正在查询,请稍候...");
$.ajax({
url:'query.php',
type:'post',
dataType:'json',
data:{queryType:'all'}, // 设置查询类型变量,让后台程序区分是全部查询还是条件查询
//查询成功,调用函数返回结果
success:showQueryResult,
//查询失败,显示提示
error:function(){
$("#queryResult").html("没有查询到结果。");	
}
})
})
})
// 条件查询
$(function(){
$("#button3").click(function(){
$("#queryResult").text("正在查询,请稍候...");
txtField=$("#queryField").val();
txtKeyword=$("#queryKeywords").val();
$.ajax({
url:'query.php',
type:'post',
dataType:'json',
data:{queryFields:txtField,queryKeywords:txtKeyword},
//查询成功,调用函数返回结果
success:showQueryResult,
//查询失败,显示提示
error:function(){
$("#queryResult").html("没有查询到结果。");	
}
})
})
})	

//客户验证
$(function(){
$("#button5").click(function(){
$("#validateResult").text("正在验证,请稍候...");
txtUserName=$("#txtUserName2").val();
txtUserTel=$("#txtUserTel2").val();
txtGoodsID=$("#txtGoodsID2").val();
$.ajax({
url:"validate.php",
type:"POST",
dataType:"html",
data:{postUserName:txtUserName,postUserTel:txtUserTel,postGoodsID:txtGoodsID},
success:function(data){	
if(parseInt(data)>0)	//后台程序会将查询结果记录的条数返回到这里,根据此值检查是否查询到记录
$("#validateResult").html("验证成功!根据您提供的信息,我们认定为有效交易,");
else
$("#validateResult").html("对不起,我们无法验证您输入的信息的有效性,验证失败。");
},
error: function(){
$("#validateResult").html("查询出错,请联系网站管理员");
}
})
})
})	
//隐藏查询结果
$(function(){
$("#button4").click(function(){
$("#queryResult").html("");
})
})
//显示查询结果
function showQueryResult(data)
{	
var str="<table border='1'><tr><td>编号</td><td>客户姓名</td><td>客户电话</td><td>货物编号</td><td>货物名称</td><td>货物数量</td><td>交易日期</td>";
$.each(data,function(index,info)
{
str+="<tr><td>"+info["ID"]+"</td><td>"+info["userName"]+"</td><td>"+info["userTel"]+"</td><td>"+info["goodsID"]+"</td><td>"+info["goodsName"]+"</td><td>"+info["goodsNum"]+"</td><td>"+info["tradeDate"]+"</td></tr>";
})
$("#queryResult").html(str+"</table>");	
}

insert.php //新增记录后台程序

<?php
$insertSQL="insert into tradeDetails values(null,'".$_POST['postUserName']."','".$_POST['postUserTel']."','".$_POST['postDate']."','".$_POST['postGoodsID']."','".$_POST['postGoodsName']."','".$_POST['postGoodsNum']."')";
mysql_connect('localhost','root','root');
mysql_query("set names utf8");
mysql_select_db("test");
echo mysql_query($insertSQL);
?>

query.php //查询功能后台程序

<?php
mysql_connect("localhost","root","root");
mysql_query("set names utf8");
mysql_select_db("test");
$querySql="";
if(isset($_POST['queryType'])==true) //获取查询类型,如果queyType变量存在,则说明是全部查询
{
$querySql="select * from tradeDetails order by ID";
}
else // 否则,是条件查询
{
$querySql="select * from tradeDetails where ".$_POST['queryFields']."='".$_POST['queryKeywords']."'";
}
$myrs=mysql_query($querySql);


while($row=mysql_fetch_array($myrs,MYSQL_ASSOC))
{
$arr[]=$row;
}
echo json_encode($arr);
?>

validate.php //客户功能后台程序

<?php
mysql_connect("localhost","root","root");
mysql_query("set names utf8");
mysql_select_db("test");
$querySql="SELECT ID FROM tradeDetails WHERE userName = '".$_POST['postUserName']."' and userTel='".$_POST['postUserTel']."' AND goodsID ='".$_POST['postGoodsID']."'";
$rs=mysql_query($querySql);
$totalRows_myrs=mysql_num_rows($rs);
echo $totalRows_myrs; //返回查询到的记录条数
?>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值