sql html 查询 变色,反映在SQL数据库实时更改为HTML

当您使用$.post这是一个简写为$.ajax,function(data)是什么时候ajax成功。

而不是只有console.log(data)你可以在该函数中的代码来更新你的#profile div。理想的方法是让你的editprofile.php返回fname lname和联系人作为json字符串(这很容易,下面有一个例子)清理到ajax调用(data),并用它来填充#profile div。

editprofile.php:你的数据库的逻辑后,使其返回一个JSON字符串:

//Make sure this is the onlything echoed in the php file.

//Sanitize your $_POST first im not doing it here but you should be careful with that;

echo json_encode($_POST);

?>

的Javascript:

$("#editButton").click(function(){

$.post("editprofile.php",

{

fname: $("#fnameBox").val(),

lname: $("#lnameBox").val(),

contact: $("#contactBox").val()

},

function(data){

try{

jdata = JSON.parse(data);

$('#profile').html('Name: '+jdata.fname+' '+jdata.lname+'
Contact Number'+jdata.contact);

} catch(e){

//code to manage the error

//If you are 100% sure the editprofile will work and don't want to catch the errors

//Just use the code inside the try{} and forget the try...catch

}

});

});

顺便说一句,而不是越来越领域的.val()单独的,你可以使用.serialize()仅针对表单:

//This would do the same as the code above without trying to catch the error:

$.post("editprofile.php", $('#myForm').serialize(), function(data){

jdata = JSON.parse(data);

$('#profile').html('Name: '+jdata.fname+' '+jdata.lname+'
Contact Number'+jdata.contact);

});

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值