$.ajax
의 약자 인 $.post
을 사용하면 function(data)
은 아약스가 성공했을 때 발생합니다.
console.log(data)
대신 #profile div를 업데이트하는 코드가있을 수 있습니다. 이상적인 방법은 editprofile.php
이 fname lname을 반환하고 ajax 호출 (데이터)에 json 문자열로 위생 처리 된 연락처 (아래 예제가 있음)를 사용하고 #profile div를 채우는 데 사용하는 것입니다.
editprofile.php :
<?php
//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);
?>
자바 스크립트 : 데이터베이스 논리 후에는 JSON 문자열 반환 할 수 있도록 개별적으로 필드의 .val()
을 얻는 대신 그런데
$("#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+'<br> 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
}
});
});
당신 양식을 타겟팅하는 .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+'<br> Contact Number'+jdata.contact);
});
오 세상에 이것은 aw입니다. ~. jQuery에서 배울 점이 많으며 이제는 그 목표에 한 걸음 더 다가 가고 있습니다. 정말 고맙습니다. – Gannicus