Jquery ajax 完整实例子1

 

$ajax请求---------------------------------

var $personWifePs=$("#wife-money tbody tr");
var $personWife=$("#wife-money").find("input.write");
var $val=null;
$.ajax({
type: "POST",
url: "data/hm_monery_serceh.php",
success: function(dtmonery){
var $list = JSON.parse(dtmonery);
for(info in $list) {
$personWifePs.append(`<td>
<input id="d${n+=1}" value="${$list[info].monery}" class="write" type="text"/>
</td>`)
$personWife.val($list[info].monery);
}
}
});

$personWifePs.on("change",".write",function(){
var $iput=$(this);
$val=$(this).val();
var $index=$personWifePs.children().find("input").index($(this)[0]);
$.ajax({
type: "POST",
url: "data/hm_monery_update.php",
data: "id="+($index+1)+"&monery="+$val,
success: function(dtmonery){
var $listone = JSON.parse(dtmonery);
$iput.val($listone.monery);
}
});
});

PHP公共---------------------------------

<?php
//1:将连接数据库与设置编码程序保存
// init.php中
//2:哪个php需要数据库连接
// require('init.php');
//require加载-->init.php
$conn = mysqli_connect('127.0.0.1','root','','home');
$sql = "SET NAMES UTF8";
mysqli_query($conn,$sql);
?>

更新PHP-------------------

<?php
//1:修改响应数据类型
header("Content-Type:text/plain;charset=utf-8");
//2:获取所有参数
@$n = $_REQUEST['id'] or die("-2");
@$m = $_REQUEST['monery'] or die("-3");
//3:连接数据库 设置编码
require("init.php");
//4:创建sql 发送sql
$sql = "UPDATE hm_moneryone SET monery =$m WHERE id = $n ";
$result = mysqli_query($conn,$sql);

$sql = "SELECT * FROM hm_moneryone WHERE id=$n";
$result = mysqli_query($conn,$sql);
$back = mysqli_fetch_assoc($result);
$json = json_encode($back);
echo $json;
?>

查询PHP--------------------

<?php
//0:修改响应头消息 text/html
header("Content-Type:text/html;charset=utf-8");
//2:连接数据库 设置编码
require("init.php");
//3:创建sql 发送sql
$sql = "SELECT * FROM hm_moneryone";
//4:抓取多行记录
$result = mysqli_query($conn,$sql);
$rows = mysqli_fetch_all($result,MYSQLI_ASSOC);
$json = json_encode($rows);
echo $json;
?>

posted @ 2017-08-31 23:21  ThisCall  阅读(581)  评论(0编辑  收藏  举报